使用其他表中的值更新表中的值,并基于data.table方式中其他表中的条件

时间:2017-03-17 05:48:50

标签: r join data.table updates

我有两个data.table

Warning message:
In `[.data.table`(DT2, DT1$Code == 3, `:=`(Part, DT1$PartTo)) :
Supplied 3 items to be assigned to 1 items of column 'Part' (2 unused)

并希望根据第二个表中的条件使用来自第二个表的值更新第一个表中的值。

我尝试了以下内容,但它不会更新为" D",而是更新为NA:

    Part Info
1:    A    1
2:   NA    3
3:    C    1

带有警告信息:

$data['hello']= "helloooo";
$this->load->view('hello_world', $data);  

结果:

{{1}}

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我们可以将'Code'为3的第一个数据集,使用'DT2'连接on'Part',并将DT1中的'PartTo'分配给:='部分“

DT2[DT1[Code==3], Part := PartTo, on = .(Part)]
DT2
#   Part Info
#1:    A    1
#2:    D    3
#3:    C    1