I have the following data frame:
df<-data.frame(Date=rep(x=as.Date(c("2016/01/01", "2016/01/02")),each=12),
Group=rep(x=c("G1","G2"),each=6,times=2),
ID=rep(c("G1F1","G1F2","G1F3","G1M4","G1M5","G1M6","G2F1","G2F2","G2F3","G2M4","G2M5","G2M6"),times=2),
Gender=rep(c("Female","Male"),each=3,times=4),
Weight=c(c(100,100,100,100,120,140),rep(c(100,120,140,100,100,100),times=2),c(100,100,100,100,120,140)))
I would like to create a new column CountCompthat displays a count of the same number of members of the same group of group members whose weight is within 10 grams above or below the weight of the individual indicated in each row for a specific date, such as get:
df2<-data.frame(Date=rep(x=as.Date(c("2016/01/01", "2016/01/02")),each=12),
Group=rep(x=c("G1","G2"),each=6,times=2),
ID=rep(c("G1F1","G1F2","G1F3","G1M4","G1M5","G1M6","G2F1","G2F2","G2F3","G2M4","G2M5","G2M6"),times=2),
Gender=rep(c("Female","Male"),each=3,times=4),
Weight=c(c(100,100,100,100,120,140),rep(c(100,120,140,100,100,100),times=2),c(100,100,100,100,120,140)),
CountComp=c(c(2,2,2,0,0,0),rep(c(0,0,0,2,2,2),times=2),c(2,2,2,0,0,0)))
I am very new to R and thought I dplyrcould offer a solution using group_byboth mutatefunctions, but still could not find it.