Merging all column by reference in a data.table

旧街凉风 提交于 2020-01-03 16:39:04

问题


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

I want to copy every columns of b into a by reference without specifying the column names.

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

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

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.

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.


回答1:


As you wrote, a combination of colnames and mget could get you there.

Consider this:

# 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?

Besides, I don't think another syntax is currently implemented with data.table. But I would be happy to be proven wrong :)



来源:https://stackoverflow.com/questions/45043600/merging-all-column-by-reference-in-a-data-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!