data.table 连接(多个)具有新名称的选定列 [英] data.table join (multiple) selected columns with new names
问题描述
我喜欢连接两个表,它们有一些相同的列(名称和值),而另一些则没有.我只对加入那些不相同的人感兴趣,我想为他们确定一个新名字.对于我拥有 100 多列的真实表,我目前的做法似乎很冗长且难以处理,即我想提前确定要连接的列,而不是在 join 语句中.可重现的例子:
I like to join two tables that have some identical columns (names and values) and others that are not. I'm only interested in joining those that are not identical and I would like to determine a new name for them. The way I currently do it seems verbose and hard to handle for the real tables I have with 100+ columns, i.e. I would like to determine the columns to be joined in advance and not in join statement. Reproducible example:
# create table 1
DT1 = data.table(id = 1:5, x=letters[1:5], a=11:15, b=21:25)
# create table 2 with changed values for a, b via pre-determined cols
DT2 = copy(DT1)
cols <- c("a", "b")
DT2[, (cols) := lapply(.SD, function(x) x*2), .SDcols = cols]
# this both works but is verbose for many columns
DT1[DT2, c("a_new", "b_new") := list(i.a, i.b), on=c(id="id")]
DT1[DT2, `:=` (a_new=i.a, b_new=i.b), on = c(id="id")]
我在想这样的事情(不起作用):
I was thinking about something like this (doesn't work):
cols_new <- c("a_new", "b_new")
cols <- c("a", "b")
DT1[DT2, cols_new := i.cols, on=c(id="id")]
推荐答案
根据 Arun 的建议更新答案:
Updated answer based on Arun's recommendation:
cols_old <- c('i.a', 'i.b')
DT1[DT2, (cols_new) := mget(cols_old), on = c(id = "id")]
您也可以通过以下方式生成 cols_old
:
you could also generate the cols_old
by doing:
paste0('i.', gsub('_new', '', cols_new, fixed = TRUE))
<小时>
查看历史以获取旧答案.
See history for the old answer.
这篇关于data.table 连接(多个)具有新名称的选定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!