[R] Question on Counting Factors

Daniel Gabrieli daniel.gabrieli at gmail.com
Wed Apr 11 15:21:27 CEST 2012


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,

animals = as.data.frame(animals)


# 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

Any help is much appreciated.



More information about the R-help mailing list