[R] Transforming relational data
Matthew Dowle
mdowle at mdowle.plus.com
Wed Feb 23 01:38:02 CET 2011
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!
More information about the R-help
mailing list