R is data.table, and then add all the columns from one table to another

My question is essentially the same as this question: data.table join, then add the columns to the existing data.frame file without a duplicate .

Basically, I have a template with keys, and I want to assign columns from other data.tables to the template with 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
> 

But if you xand ysay 100 columns, then it is impossible to write the syntax value := i.valuefor all columns. Is there a way to do the same, but for all columns in xand y?

EDIT: If I do y[x[template]], then it creates separate columns valuethat are 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
> 
+4
source share
1

, . eval , data.table, . :

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
+6

Source: https://habr.com/ru/post/1534298/


All Articles