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\
Let's not forget that a lot of things can be done in base
R, although sometimes not as efficiently as data.table
or dplyr
:
df$count<-ave(as.integer(df$one),df[,2:3],FUN=function(x) sum(!is.na(x)))
# one group1 group2 count
#1 one A C 1
#2 A C 1
#3 A C 1
#4 A D 0
#5 B E 1
#6 two B E 1
#7 B F 0
#8 B F 0