我希望在单个join语句中将多个表链接在一起。即table a
联接到table b
联接到table c
。
# create some tables
a = data.table(id = c(1,2,3,4),amount = c(2,2,3,4))
b = data.table(id=c(1,2,3,4), id2=c(1,1,2,2), multiplier = c(4,5,6,7))
c = data.table(id=c(1,2,3,4))
d = data.table(id2=c(1,2), amount = c(2,20))
接下来,我可以将表c
联接到a
和b
c[b, on = .(id), `:=` (multiplier = i.multiplier, m2 = multiplier*am)][a, on = .(multiplier==id), am := i.amount]
id multiplier am m2 1: 1 4 4 16 2: 2 5 NA NA 3: 3 6 NA NA 4: 4 7 NA NA
如何将表b
联接到d
,然后再将c
联接到b
?
以下内容无效:
c[b, on = .(id), `:=` (multiplier = i.multiplier, m2 = multiplier*am)][d, on = .(id2==id2), am := i.amount]
c[b, on = .(id), `:=` (multiplier = i.multiplier, m2 = multiplier*prod(b[d, on = .(id2), amount])]
答案 0 :(得分:0)
我弄错了方向:
c[, m2 := b[, by=id, multiplier*prod(d[.SD, on =.(id2), amount])]$V1]