I am trying to perform an update join of two data tables with the fields (more than one) I need to use to join stored in a variable. Below is an example:
library(data.table)
DT1 <- data.table(col1 = 1:5, col2 = 5:1, lett = letters[1:5])
DT2 <- data.table(col1 = c(1:3, 2:5, 1), col2 = c(5:3, 4:1, 5))
joinFields <- c('col1', 'col2')
I tried doing it this way:
DT1[DT2,
on=c(paste0(joinFields, '=', joinFields)),
nomatch=0L]
This way is based on a solution suggested in Join datatables using column names stored in variables.
dt1[dt2_temp,
on=c(paste0(varName, ">valueMin"), paste0(varName, "<=valueMax")),
nomatch=0L]
It does not work. Obviously, my case is a bit different, because in the example I used, there are 2 pastes. Is there a solution that continues to allow me using on = c()?
Edit: I am aware I can do it with merge()`
:=. Could you show and explain the desired output? Btw, you might need"=="not"="there, I guess, egDT1[DT2, on=sprintf("%s==%s", joinFields, joinFields)]DT1[DT2, on=joinFields, nomatch=0L]?let := i.lett