r - data.table join,然后将所有列从一个表添加到另一个表 [英] r - data.table join and then add all columns from one table to another

查看:1077
本文介绍了r - data.table join,然后将所有列从一个表添加到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题本质上与这个问题相同: data.table join然后将列添加到现有的data.frame而不重新复制

My question is essentially the same as this question: data.table join then add columns to existing data.frame without re-copy.

基本上我有一个模板并且我想通过相同的键将其他data.tables的列分配给模板。

Basically I have a template with keys and I want to assign columns from other data.tables to the template by the same keys.

> template
    id1 id2
 1:   a   1
 2:   a   2
 3:   a   3
 4:   a   4
 5:   a   5
 6:   b   1
 7:   b   2
 8:   b   3
 9:   b   4
10:   b   5
> x
   id1 id2       value
1:   a   2  0.01649728
2:   a   3 -0.27918482
3:   b   3  0.86933718
> y
   id1 id2     value
1:   a   4 -1.163439
2:   b   4  2.267872
3:   b   5  1.083258
> template[x, value := i.value]
> template[y, value := i.value]
> template
    id1 id2       value
 1:   a   1          NA
 2:   a   2  0.01649728
 3:   a   3 -0.27918482
 4:   a   4 -1.16343917
 5:   a   5          NA
 6:   b   1          NA
 7:   b   2          NA
 8:   b   3  0.86933718
 9:   b   4  2.26787248
10:   b   5  1.08325793
> 

但如果 x y 有100列,那么不可能为所有列写出 value:= i.value 语法。是否有办法做同样的事情,但对于 x y

But if x and y have say 100 columns, then it is not possible to write out the value := i.value syntax for all columns. Is there a way to do the same thing but for all the columns in x and y?

编辑:
如果我做 y [x [template]] ,则会创建单独的 value 列,这不是为了:

If I do y[x[template]], then it creates separate value columns, which is not intended:

> y[x[template]]
    id1 id2     value     value.1
 1:   a   1        NA          NA
 2:   a   2        NA  0.01649728
 3:   a   3        NA -0.27918482
 4:   a   4 -1.163439          NA
 5:   a   5        NA          NA
 6:   b   1        NA          NA
 7:   b   2        NA          NA
 8:   b   3        NA  0.86933718
 9:   b   4  2.267872          NA
10:   b   5  1.083258          NA
> 


推荐答案

只需创建一个函数,你的表情。然后每次通过传递每个 data.table 的名称 eval 。这里有个例子:

Just create a function that takes names as arguments and constructs the expression for you. And then eval it each time by passing the names of each data.table you require. Here's an illustration:

get_expr <- function(x) {
    # 'x' is the names vector
    expr = paste0("i.", x)
    expr = lapply(expr, as.name)
    setattr(expr, 'names', x)
    as.call(c(quote(`:=`), expr))
}

> get_expr('value')    ## generates the required expression
# `:=`(value = i.value)

template[x, eval(get_expr("value"))]
template[y, eval(get_expr("value"))]

#     id1 id2       value
#  1:   a   1          NA
#  2:   a   2  0.01649728
#  3:   a   3 -0.27918482
#  4:   a   4 -1.16343900
#  5:   a   5          NA
#  6:   b   1          NA
#  7:   b   2          NA
#  8:   b   3  0.86933718
#  9:   b   4  2.26787200
# 10:   b   5  1.08325800

这篇关于r - data.table join,然后将所有列从一个表添加到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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