R by command for multiple tables

June 21, 2011 by
Filed under: Notes 

If you want to summarize a breakdown of counts by various groups in a single data frame:

x <- as.integer(runif(1000)*5)
y <- rep(c("A", "B", "C", "D"), 250)
do.call(rbind, by(x, list(y), function(g) {table(g)}))

results in:
'  0  1  2  3  4
A 54 45 45 51 55
B 52 51 56 42 49
C 60 50 49 43 48
D 51 51 51 50 47

Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!