I have the following dataframe
one <- c(\'one\',NA,NA,NA,NA,\'two\',NA,NA) group1 <- c(\'A\',\'A\',\'A\',\'A\',\'B\',\'B\',\'B\',\'B\') group2 <- c(\'C\
library(dplyr) df %>% group_by(group1, group2) %>% mutate(count = sum(!is.na(one)))
Source: local data frame [8 x 4] Groups: group1, group2 [4] one group1 group2 count 1 one A C 1 2 NA A C 1 3 NA A C 1 4 NA A D 0 5 NA B E 1 6 two B E 1 7 NA B F 0 8 NA B F 0