After some research, especially here, I discovered what I think is a very interesting method to join two data.table by condition :
DT[WHERE, v := FROM[.SD, on=, x.v]] .
Unfortunately, I can not perform this in spite of many attempts.
From DT1 and DT2, I need to create DTres : perform a join only on some DT1 rows.
And DT3 is one of my unsuccessful attempt...
Is it possible to do this ? How to ?
Many thanks for helping.
library(data.table)
DT1 <- data.table(crit = rep(c('AA', 'BB', 'CC', 'DD'),each = 3),
num = rep(1:3, 4),
val = rnorm(12)^2)
DT1
DT2 <- data.table(BB = c(1,3),
cross = c(128, 183))
DT2
DTres <- data.table(crit = rep(c('AA', 'BB', 'CC', 'DD'),each = 3),
num = rep(1:3, 4),
val = rnorm(12)^2,
cross = c(rep(NA,3), 128, NA, 183, rep(NA, 6))
)
DTres
DT3 <- DT1[crit == 'BB', cross := DT2[DT1, on = .('BB' = num), x.cross]]