I have two data.tables DT1
and DT2
. I want to perform DT1 = DT1[DT2]
without duplicating the data.table.
More specifically, I'm trying to update a subset of DT1
by adding columns from DT2
where there are matches. The default output NA
for rows where there is no match is perfectly fine (I want to retain all rows of DT1
). If I order the two data.tables properly with the same keys, DT1[DT2,c("col1","col2"):=list(DT2$col1,DT2$col2)]
works (in the cases I've looked at: ie. no duplicates), but would become tedious with many columns.
Is there an easy way to do this?
(merge-like scenario with two data.tables addresses adding a single column and Merging two data.tables with many to one realtionship in R? doesn't address memory efficiency.)
Example:
dt = data.table(col1 = 1:3)
dt2 = data.table(col1 = 2:5,col2 = 3:6,col3=c("a","b","c","d"))
setkey(dt,col1)
setkey(dt2,col1)
Join dt
and dt2
to add col2 and col3 to dt
without making a copy of dt.