How to count how many values per level in a given factor?
Or using the dplyr
library:
library(dplyr)set.seed(1)dat <- data.frame(ID = sample(letters,100,rep=TRUE))dat %>% group_by(ID) %>% summarise(no_rows = length(ID))
Note the use of %>%
, which is similar to the use of pipes in bash. Effectively, the code above pipes dat
into group_by
, and the result of that operation is piped into summarise
.
The result is:
Source: local data frame [26 x 2] ID no_rows1 a 22 b 33 c 34 d 35 e 26 f 47 g 68 h 19 i 610 j 511 k 612 l 413 m 714 n 215 o 216 p 217 q 518 r 419 s 520 t 321 u 822 v 423 w 524 x 425 y 326 z 1
See the dplyr
introduction for some more context, and the documentation for details regarding the individual functions.
Here 2 ways to do it:
set.seed(1)tt <- sample(letters,100,rep=TRUE)## using tabletable(tt)tta b c d e f g h i j k l m n o p q r s t u v w x y z 2 3 3 3 2 4 6 1 6 5 6 4 7 2 2 2 5 4 5 3 8 4 5 4 3 1 ## using tapplytapply(tt,tt,length)a b c d e f g h i j k l m n o p q r s t u v w x y z 2 3 3 3 2 4 6 1 6 5 6 4 7 2 2 2 5 4 5 3 8 4 5 4 3 1