I have the following data.table:
DT <- data.table(id=c("A","A","B","B","C","C"),condition=c(1,2,1,2,1,2),value=c(0,1,1,3,2,2))
For each value of id and condition, I want to compute the mean value for that condition but for all values of id other than the current one. My current solution is:
DT[,meanothers:=DT[id!=ID & condition==CONDITION,mean(value)],by=.(ID=id,CONDITION=condition)]
Is there a faster or more memory-efficient solution to this problem in data.table?
Using the idea from one of my old answers: