我有一个data.table DT1
,其中包含每个区域的值。在配置表DT2
中,我将每个区域的每个值映射到变量number_carrots
。
是否有一种优雅的data.table方法(根据number_carrots
中的映射向DT1
添加列(DT2
)?我只能通过循环来做到这一点...
举个例子。 DT1
是:
library(data.table)
region1 = c('Europe', 'Europe', 'Africa', 'Africa', 'Europe', 'Africa')
value1 = c(1, 2, 1, 2, 3, 1)
DT1 = data.table(region1, value1)
> DT1
region1 value1
1: Europe 1
2: Europe 2
3: Africa 1
4: Africa 2
5: Europe 3
6: Africa 1
DT2
是:
region2 = rep(c('Europe', 'Africa'), each = 3)
value2 = c(1:3, 1:3)
number_carrots = c(10, 20, 30, 5, 15, 30)
DT2 = data.table(region2, value2, number_carrots)
> DT2
region2 value2 number_carrots
1: Europe 1 10
2: Europe 2 20
3: Europe 3 30
4: Africa 1 5
5: Africa 2 15
6: Africa 3 30
使用DT2
的映射,我想向number_carrots
添加一列DT1
:
> DT1
region1 value1 number_carrots
1: Europe 1 10
2: Europe 2 20
3: Africa 1 5
4: Africa 2 15
5: Europe 3 30
6: Africa 1 5
答案 0 :(得分:2)
OP已请求在number_carrots
中添加一列DT1
。
这可以通过 update join 解决,该更新通过引用修改DT1
DT1[DT2, on = .(region1 = region2, value1 = value2), number_carrots := i.number_carrots]
DT1
region1 value1 number_carrots 1: Europe 1 10 2: Europe 2 20 3: Africa 1 5 4: Africa 2 15 5: Europe 3 30 6: Africa 1 5