dplyr count non-NA value in group by [duplicate]
问题 This question already has answers here : R group by, counting non-NA values (3 answers) Closed last year . Here is my example mydf<-data.frame('col_1'=c('A','A','B','B'), 'col_2'=c(100,NA, 90,30)) I would like to group by col_1 and count not-NA elements in col_2 I would like to do it with dplyr . Here is what I tried after searching SO: mydf %>% group_by(col_1) %>% summarise_each(funs(!is.na(col_2))) mydf %>% group_by(col_1) %>% mutate(non_na_count = length(col_2, na.rm=TRUE)) mydf %>% group