Rdatatable / data.table

R's data.table package extends data.frame:
http://r-datatable.com
Mozilla Public License 2.0
3.62k stars 986 forks source link

non-equi join + rolling join #1929

Open ben519 opened 7 years ago

ben519 commented 7 years ago

I happen to need a non-equi join combined with a rolling join. Something like

library(data.table)  # version 1.9.9
dt1 <- data.table(ID1=1:10, a = seq(1, 10, by=1.0), b = seq(1, 10, by=1.0), Roll = seq(1, 10, by=1.0))
dt2 <- data.table(ID2=1:8, A=c(1,1,1,1,5,5,5,5), B=c(5,5,10,10,8,8,10,10), Roll=c(2,4,6,8,2,4,6,8))

dt1
dt2
dt1[dt2, on=c("a>=A", "b<=B", "Roll"), roll=TRUE]   # Error: roll is not implemented for non-equi joins yet.

I can get around this for now, but I think it'd be a cool new feature.

franknarf1 commented 7 years ago

Another example from SO: OP only cares about the first match within 30 days here.

The current alternative (probably what Ben had in mind...) is to sort, join with on = .(v > v), mult = "first", convert to NA if x.v - i.v > 30. I guess that's less efficient than a roll could be.