data.table join - 选择i参数中的所有列 [英] data.table joins - Select all columns in the i argument

查看:129
本文介绍了data.table join - 选择i参数中的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加入两个data.table我可以指定我想要的列的表,例如

Joining two data.table I can specify the table I want the column from, like

X[Y, i.id] # `id` is taken from Y

我的问题是我有一个大表, 80列。每天晚上发生数据刷新,根据一些参数,一些行被新版本的表(同一个表,只是新数据)替换。

My problem is that I have a big table with ~80 columns. Every night a data refresh happens and, according to some parameters, some rows get replaced by a new version of the table (same table, just new data).

current <- data.table(id=1:4, var=1:4, var2=1:4, key="id")
new <- data.table(id=1:4, var=11:14, var2=11:14, key="id")
current[new[c(1,3)], `:=`(var=i.var, var2=i.var2)]
> current
   id var var2
1:  1  11   11
2:  2   2    2
3:  3  13   13
4:  4   4    4

正如我所说,在我的实际情况下,我有更多的列这样(除了rbind我不知道如何选择在连接中使用的 data.table 的所有列作为i参数?我可以花费半个小时硬编码所有的,但它不会是一个可维护的代码(如果将来新的列添加到表中)。

As I said, in my real case, I have much more columns so (besides rbind()ing pieces of the two tables) I wonder how can I select all the columns of the data.table used in a join as the i argument? I could spend an half an hour in hard coding all of them but it wouldn't be a maintainable code (in case new columns get added to the tables in future).

推荐答案

如何构建 j-expression 和只是 eval

nc = names(current)[-1L]
nn = paste0("i.", nc)
expr = lapply(nn, as.name)
setattr(expr, 'names', nc)
expr = as.call(c(quote(`:=`), expr))

> current[new[c(1,3)], eval(expr)]
> current
##    id var var2
## 1:  1  11   11
## 2:  2   2    2
## 3:  3  13   13
## 4:  4   4    4

这篇关于data.table join - 选择i参数中的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆