我有2个data.table
,我想用另一个按组来制作一个产品。2个数据表的排序产品
library(data.table)
set.seed(1)
DT <- as.data.table(matrix(rnorm(16),ncol=4))
DT[,id:=c(1,1,2,2)]
DT2 <- as.data.table(matrix(rnorm(8),ncol=4))
DT2[,id:=c(1,2)]
#DT
# V1 V2 V3 V4 id
#1: -0.6264538 0.3295078 0.5757814 -0.62124058 1
#2: 0.1836433 -0.8204684 -0.3053884 -2.21469989 1
#3: -0.8356286 0.4874291 1.5117812 1.12493092 2
#4: 1.5952808 0.7383247 0.3898432 -0.04493361 2
#DT2
# V1 V2 V3 V4 id
#1: -0.01619026 0.8212212 0.9189774 0.07456498 1
#2: 0.94383621 0.5939013 0.7821363 -1.98935170 2
cols <- grep(colnames(DT2), pattern='V.*', value=T)
ids <- DT2[,unique(id)]
for (id_i in ids) {
l <- as.matrix(DT[id==id_i,(cols),with=F])
r <- diag(t(DT2[id==id_i,(cols),with=F])[,1L])
DT[id==id_i,(cols):=as.data.table(l%*%r)]
}
#DT(i,j) = DT(i,j)*DT2(j) with id matching
V1 V2 V3 V4 id
1: 0.010142452 0.2705988 0.5291300 -0.04632279 1
2: -0.002973234 -0.6737860 -0.2806450 -0.16513906 1
3: -0.788696543 0.2894848 1.1824189 -2.23788323 2
4: 1.505683787 0.4384920 0.3049105 0.08938875 2
必须有办法做到这一点有效地利用by
和.EACHI
但解决的办法是躲避我
如果'DT [,(cols):= as.data.table(l%*%r)]'是DT [id_i,(cols):= as.data.table(l%*%r) ]'? – lmo
@lmo是的,你完全正确 – statquant
如何使用矩阵的matricial产品? –