r - dplyr: put count occurences into new variable -



r - dplyr: put count occurences into new variable -

would hand on dplyr code, cannot figure out. have seen similar issue described here many variables (summarizing counts of factor dplyr , putting rowwise counts of value occurences new variables, how in r dplyr?), task smaller. given info frame, how count frequency of variable , place in new variable.

set.seed(9) df <- data.frame( group=c(rep(1,5), rep(2,5)), var1=round(runif(10,1,3),0))

then have:

>df grouping var1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 2 6 2 1 7 2 2 8 2 2 9 2 2 10 2 3

would 3rd column indicating per-group (group) how many times var1 occurs, in illustration be: count=(4,4,4,4,1,1,3,3,3,1). tried - without success - things like:

df %>% group_by(group) %>% rowwise() %>% do(count = nrow(.$var1))

explanations appreciated!

all need grouping info both columns, "group" , "var1":

df %>% group_by(group, var1) %>% mutate(count = n()) #source: local info frame [10 x 3] #groups: group, var1 # # grouping 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 illustration of how should not it:

df %>% group_by(group, var1) %>% do(data.frame(., count = length(.$group)))

the dplyr implementation n() sure much faster, cleaner , shorter , should preferred on such implementations above.

r dplyr

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -