I have this two table
library(data.table)
n = 100
set.seed(12)
d = runif(n)
dtBig = data.table(id = rep(letters[1:2],each = n/2),
d1 = d,
d2 = d + runif(n))
dtSmall = data.table(id = rep(letters[1:2],each = 2),
d_start = c(0.2,0.65,0.15,1.1),
d_end = c(0.65,0.85,0.8,1.5))
I would like to do a merge with two conditions on d1 and d2.
I tried that
dtAll = merge(dtSmall, dtBig, by = "id", allow.cartesian = T)
dtAll = dtAll[d1 >= d_start & d2 <= d_end]
However these command create a big table with all possible merge. In this case the tables are small so it's ok but in my case I have so many rows. So I look into the on join like this
dtAll2 = dtSmall[,dtBig[.SD, on = .(id,d1>=d_start)]]
dtAll2 = dtAll[d2 <= d_end]
However, only one condition is allowed with on.
Do you know a command in one line (without saving the big table) ?
Thank for your help.