Data.table join - select all columns in argument i

Join two data tables. I can specify the table in which I want to get the column, for example

X[Y, i.id] # `id` is taken from Y

My problem is that I have a large table with ~ 80 columns. Every night, data is updated and, according to some parameters, some rows are replaced by a new version of the table (the same table, only new data).

current <- data.table(id=1:4, var=1:4, var2=1:4, key="id")
new <- data.table(id=1:4, var=11:14, var2=11:14, key="id")
current[new[c(1,3)], `:=`(var=i.var, var2=i.var2)]
> current
   id var var2
1:  1  11   11
2:  2   2    2
3:  3  13   13
4:  4   4    4

As I said, in my real case I have a lot more columns, so (besides rbind () for fragments of two tables) I wonder how I can select all the columns data.tableused in the join, how am I an argument? I could spend half an hour on hard coding, but this will not be supported code (in case new columns are added to the tables in the future).

+3
1

j-expression eval ' ?

nc = names(current)[-1L]
nn = paste0("i.", nc)
expr = lapply(nn, as.name)
setattr(expr, 'names', nc)
expr = as.call(c(quote(`:=`), expr))

> current[new[c(1,3)], eval(expr)]
> current
##    id var var2
## 1:  1  11   11
## 2:  2   2    2
## 3:  3  13   13
## 4:  4   4    4
+5

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


All Articles