R中的条件合并/替换:按程序进行

时间:2019-06-20 20:52:35

标签: r join merge data.table

我的问题与这篇文章有关:Conditional merge/replacement in R

我正在使用此处引用的data.table解决方案。唯一的问题是,我需要在循环中执行此操作。本质上,我只需要以下代码即可工作:

library(data.table)
df1 = data.table(x1 = c(1, 2, 3, 4, 5), x2 = c('a', 'b', 'c', 'd', 'e'))
df2 = data.table(x1 = c(1, 2, 3, 4), x2 = c('a', 'x', 'x', 'd'))
z = df1[df2, on = .(x1), x2 := i.x2] # Works perfectly

varname = 'x2'
z = df1[df2, on = .(x1), get(varname) := i.get(varname)] # Doesn't work

0 个答案:

没有答案