数据表在单个联接中联接多个表

时间:2020-03-04 15:11:45

标签: r data.table

我希望在单个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联接到ab

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])]

1 个答案:

答案 0 :(得分:0)

我弄错了方向:

c[, m2 := b[, by=id, multiplier*prod(d[.SD, on =.(id2), amount])]$V1]