On Apr 11, 2012, at 9:21 AM, Daniel Gabrieli wrote:
Hi,
I hope this is not too trivial, but I've had this recurring problem
and I think there is super easy solution, just not sure what it is.
Please see short example below. I would like to get the frequency
(counts) of all the variables in a single column (that is easy), but I
would also like to return the value 0 for the absence of variables
defined in another column.
For example:
animals =
matrix
(c('cat','tiger','cat','tiger','fish','fish','dog','dog'),ncol=2,
byrow=F)
animals = as.data.frame(animals)
table(animals$V1)
# Returns the count for the variables in the first column as
cat tiger
2 2
# But I would like to have table(animals$V1) return
cat tiger dog fish
2 2 0 0
You need to add the missing levels to that factor.
> levels(animals$V1) <-
unique( c('cat','tiger','cat','tiger','fish','fish','dog','dog'))
> table(animals$V1)
cat tiger fish dog
2 2 0 0
Any help is much appreciated.
Cheers,
Dan
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.