data.table 连接

编程入门 行业动态 更新时间:2024-10-18 03:21:55
本文介绍了data.table 连接 - 选择 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()ing 片段)我想知道如何选择 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'ing 它?

How about constructing the j-expression and just eval'ing it?

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 连接

本文发布于:2023-11-22 06:31:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:data   table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!