If I understand correctly, by default data.table
merges two tables by comparing columns which are set as keys and have same names. How should I write if I have
Using data.table's subset based joins along with the recently implemented on=
argument and nomatch=0L
, this is simply:
DT2[DT1, on=c(col5="col2", col4="col3"), nomatch=0L]
See the secondary indices vignette for more.
Alternatively if you've the data.tables keyed, then you can skip the on=
argument. But the solution above would be idiomatic as it retains the order of original data.tables, and it is clear to tell what columns are being looked up by looking at the code.
setkey(DT1, col2, col3)
setkey(DT2, col5, col4)
DT2[DT1, nomatch=0L]
See history for older versions.