All you need to do is group your data by both columns, "group" and "var1":
df %>% group_by(group, var1) %>% mutate(count = n())
#Source: local data frame [10 x 3]
#Groups: group, var1
#
# group var1 count
#1 1 1 4
#2 1 1 4
#3 1 1 4
#4 1 1 4
#5 1 2 1
#6 2 1 1
#7 2 2 3
#8 2 2 3
#9 2 2 3
#10 2 3 1
Edit after comment
Here's an example of how you SHOULD NOT DO IT:
df %>% group_by(group, var1) %>% do(data.frame(., count = length(.$group)))
The dplyr implementation with n()
is for sure much faster, cleaner and shorter and should always be preferred over such implementations as above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…