Preserving zero length groups with aggregate

Viewed 1840

I just noticed that aggregate disappears empty groups from the result, how can I solve this? e.g.

`xx <- c("a", "b", "d", "a", "d", "a")
 xx <- factor(xx, levels = c("a", "b", "c", "d"))
 y <- rnorm(60, 5, 1)
 z <- matrix(y, 6, 10)
 aggregate(z, by = list(groups = xx), sum)`

xx is a factor variable with 4 levels, but the result gives just 3 rows, and would like a row for the "c" level with zeros. I would like the same behavior of table(xx) tha gives frecuencies even for levels with no observations.

1 Answers
Related