根据列

时间:2018-07-30 10:01:42

标签: r data.table

我有一个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

1 个答案:

答案 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