Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table joins - Select all columns in the i argument

Tags:

r

data.table

Joining two data.table I can specify the table I want the column from, like

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

My problem is that I have a big table with ~80 columns. Every night a data refresh happens and, according to some parameters, some rows get replaced by a new version of the table (same table, just 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 much more columns so (besides rbind()ing pieces of the two tables) I wonder how can I select all the columns of the data.table used in a join as the i argument? I could spend an half an hour in hard coding all of them but it wouldn't be a maintainable code (in case new columns get added to the tables in future).

like image 891
Michele Avatar asked Mar 23 '14 19:03

Michele


1 Answers

How about constructing the j-expression and just eval'ing it?

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
like image 198
Arun Avatar answered Oct 30 '22 17:10

Arun