通过引用将所有列合并到data.table中 [英] Merging all column by reference in a data.table
问题描述
我想通过引用将两个 data.table
合并在一起,而不必写下我要合并的所有变量。这是一个了解我的需求的简单示例:
I would like to merge two data.table
together by reference without having to write down all variables I want to merge. Here is a simple example to understand my needs :
set.seed(20170711)
(a <- data.table(v_key=seq(1, 5), key="v_key"))
# v_key
#1: 1
#2: 2
#3: 3
#4: 4
#5: 5
a_backup <- copy(a)
(b <- data.table(v_key=seq(1, 5), v1=runif(5), v2=runif(5), v3=runif(5), key="v_key"))
# v_key v1 v2 v3
#1: 1 0.141804303 0.1311052 0.354798849
#2: 2 0.425955903 0.3635612 0.950234261
#3: 3 0.001070379 0.4615936 0.359660693
#4: 4 0.453054854 0.5768500 0.008470552
#5: 5 0.951767837 0.1649903 0.565894298
我想将 b
的每一列复制到通过引用 a
而不指定列名。
I want to copy every columns of b
into a
by reference without specifying the column names.
我可以执行以下操作,但是可以复制该对象无缘无故,降低了我的程序的性能并增加了所需的RAM:
I could do the following, but that would make a copy of the object for no reason, reducing the performance of my program and increasing the RAM needed :
(a <- a[b])
# v_key v1 v2 v3
#1: 1 0.141804303 0.1311052 0.354798849
#2: 2 0.425955903 0.3635612 0.950234261
#3: 3 0.001070379 0.4615936 0.359660693
#4: 4 0.453054854 0.5768500 0.008470552
#5: 5 0.951767837 0.1649903 0.565894298
另一个选择(没有无用的副本)是指定 b
,结果如下:
Another option (without useless copy) would be to specify the name of every column of b
, resulting in the following :
a <- copy(a_backup)
a[b, `:=`(v1=v1, v2=v2, v3=v3)][]
# v_key v1 v2 v3
#1: 1 0.141804303 0.1311052 0.354798849
#2: 2 0.425955903 0.3635612 0.950234261
#3: 3 0.001070379 0.4615936 0.359660693
#4: 4 0.453054854 0.5768500 0.008470552
#5: 5 0.951767837 0.1649903 0.565894298
简而言之,我想提高第二个示例(无用副本)的效率我不得不在 b
中指定每个列的名称。
In brief, I would like to have the efficiency of my second example (no useless copy) without having to specify every column names in b
.
我想我可以找到一种使用 colnames()
和 get()
函数的组合,但是我想知道是否有更清洁的方法
I guess I could find a way of doing it using a combination of the colnames()
and get()
functions, but I am wondering if there is a cleaner way to do it, syntax is so important for me.
推荐答案
如您所写,名称的组合
和 mget
可以带您到达那里。
As you wrote, a combination of colnames
and mget
could get you there.
考虑一下:
# retrieve the column names from b - without the key ('v_key')
thecols = setdiff(colnames(b), key(b))
# assign them to a
a[b, (thecols) := mget(thecols)]
这看起来还不错,不是吗?
This is not too bad-looking, is it?
此外,我认为目前 data.table
。但是我很高兴被证明是错误的:)
Besides, I don't think another syntax is currently implemented with data.table
. But I would be happy to be proven wrong :)
这篇关于通过引用将所有列合并到data.table中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!