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屋!