Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table join (multiple) selected columns with new names

Tags:

join

r

data.table

I like to join two tables that have some identical columns (names and values) and others that are not. I'm only interested in joining those that are not identical and I would like to determine a new name for them. The way I currently do it seems verbose and hard to handle for the real tables I have with 100+ columns, i.e. I would like to determine the columns to be joined in advance and not in join statement. Reproducible example:

# create table 1
DT1 = data.table(id = 1:5, x=letters[1:5], a=11:15, b=21:25)
# create table 2 with changed values for a, b via pre-determined cols
DT2 = copy(DT1)
cols <- c("a", "b")
DT2[, (cols) := lapply(.SD, function(x) x*2), .SDcols = cols]

# this both works but is verbose for many columns
DT1[DT2, c("a_new", "b_new") := list(i.a, i.b), on=c(id="id")]
DT1[DT2, `:=` (a_new=i.a, b_new=i.b), on = c(id="id")]

I was thinking about something like this (doesn't work):

cols_new <- c("a_new", "b_new")
cols <- c("a", "b")
DT1[DT2, cols_new := i.cols, on=c(id="id")]
like image 701
Triamus Avatar asked Oct 20 '25 10:10

Triamus


1 Answers

Updated answer based on Arun's recommendation:

cols_old <- c('i.a', 'i.b')
DT1[DT2, (cols_new) := mget(cols_old), on = c(id = "id")]

you could also generate the cols_old by doing:

paste0('i.', gsub('_new', '', cols_new, fixed = TRUE))

See history for the old answer.

like image 189
Dean MacGregor Avatar answered Oct 21 '25 22:10

Dean MacGregor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!