data.table:使用相同的名称替换字段中的现有值 [英] data.table: replace existing value from field with the same name
问题描述
我需要从一个data.table合并一些数据到另一个,我知道我可以
注入字段从一个data.table到另一个像这样:
df1< - data.table(Id = letters [1:5],a = 1:5)
df2< - data.table(Id = letters [1:3],a = 7:9,b = 7:9)
setkey(df1,Id)
setkey(df2,Id)
df1 [df2,b:= b ] []
#> Id a b
#> 1:a 1 7
#> 2:b 2 8
#> 3:c 3 9
#> 4:d 4 NA
#> 5:e 5 NA
但是当字段已经存在于 df1
:
df1 [df2,a:= a] []
#> Id a
#> 1:a 1
#> 2:b 2
#> 3:c 3
#> 4:d 4
#> 5:e 5
我理解df1不会被此赋值改变,因为字段 a 已在
df1
中存在,并且在赋值
的右边决定了该值,而不是 df2
中的on。那么我如何去替换 df1 $ a
中的
选择的值与 df2 $ a
得到以下结果:
#> Id a
#> 1:a 7
#> 2:b 8
#> 3:c 9
#> 4:d 4
#> 5:e 5
当通过:=
调用的RHS时,
$code> library(data.table)
df1< - data.table(Id = letters [1:5],a = 1:5)
df2< - data.table Id = letters [1:3],a = 7:9,b = 7:9)
df1 [df2,a:= ia] []
这在1.9.7中的
?data.table
中有说明。
高级:当i是data.table时,i的列可以在j中使用前缀i。 ,X [Y,(val,i.val)]。这里val指X的列和i.val Y的。
I need to merge some data form one data.table into another, and I know I can inject fields from one data.table to another like so:
df1 <- data.table(Id = letters[1:5],a = 1:5) df2 <- data.table(Id = letters[1:3],a = 7:9,b=7:9) setkey(df1,Id) setkey(df2,Id) df1[df2,b:=b][] #> Id a b #> 1: a 1 7 #> 2: b 2 8 #> 3: c 3 9 #> 4: d 4 NA #> 5: e 5 NA
but that idiom does not work when the field already exists in
df1
:df1[df2,a:=a][] #> Id a #> 1: a 1 #> 2: b 2 #> 3: c 3 #> 4: d 4 #> 5: e 5
I understand that df1 is unchanged by this assignment because the field
a
already exists indf1
and the reference toa
in the right hand side of the assignment resolves to that value, not the on indf2
. So how would I go about replacing the selected values indf1$a
with those indf2$a
to get the following:#> Id a #> 1: a 7 #> 2: b 8 #> 3: c 9 #> 4: d 4 #> 5: e 5
解决方案Use
i.
prefix when passing RHS of:=
call.library(data.table) df1 <- data.table(Id = letters[1:5],a = 1:5) df2 <- data.table(Id = letters[1:3],a = 7:9,b=7:9) df1[df2,a:=i.a][]
This is documented in
?data.table
in 1.9.7.Advanced: When i is a data.table, the columns of i can be referred to in j by using the prefix i., e.g., X[Y, .(val, i.val)]. Here val refers to X's column and i.val Y's.
这篇关于data.table:使用相同的名称替换字段中的现有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!