Thanks. How about this? DT$B = factor(DT$B) firststep = DT[,cbind(expand.grid(B,B),v=1/length(B),C=C[1]),by=A][Var1! =Var2] setkey(firststep,Var1,Var2,C) firststep = firststep[,transform(.SD,cv=cumsum(v)),by=list(Var1,Var2)] setkey(firststep,Var1,Var2,C) DT[, {x=data.table(expand.grid(B,B),C[1]-1L) firststep[x,roll=TRUE,nomatch=0][,sum(cv)] # prior familiarity },by=A] A V1 [1,] 1 0.0 [2,] 2 0.5 [3,] 3 1.5 [4,] 4 0.5
On Tue, 22 Feb 2011 05:02:05 -0800, mathijsdevaan wrote: > The output for the new example should be: > > project v > 1 0 > 2 0.5 > 3 1.5 > 4 0.5 > > The output you calculated was correct for the v per year, but the v per > group would be incorrect. I think the problem lies in the fact that > expand.grid(B,B) doesn't take into account that combinations of B can > only be formed within A. Thanks again! ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.