I'm trying to do a non-equi join in data.table
and extract the min/max of the joined values in that join.
set.seed(42)
dtA <- data.table(id=rep(c("A","B"),each=3), start=rep(1:3, times=2), end=rep(2:4, times=2))
dtB <- data.table(id=rep(c("A","B"),times=20), time=sort(runif(40, 1, 4)))
I'd like to preserve the min/max values of time
when it is between start
and end
(and on id
). Nominally this is just a non-equi join, but I can't find a combination of by=.EACHI
or mult="..."
that gets me what I want. Instead, the min/max typically are not aligned with the range I need. Unfortunately roll=
does not support non-equi ranges.
dtA[dtB, c("Min", "Max") := .(min(time), max(time)),
on = .(id, start <= time, end > time), mult = "first"]
# id start end Min Max
# <char> <int> <int> <num> <num>
# 1: A 1 2 1.011845 3.966675
# 2: A 2 3 1.011845 3.966675
# 3: A 3 4 1.011845 3.966675
# 4: B 1 2 1.011845 3.966675
# 5: B 2 3 1.011845 3.966675
# 6: B 3 4 1.011845 3.966675
dtA[dtB, c("Min", "Max") := .(min(time), max(time)),
on = .(id, start <= time, end > time), by = .EACHI]
# id start end Min Max
# <char> <int> <int> <num> <num>
# 1: A 1 2 1.858419 1.858419
# 2: A 2 3 2.970977 2.970977
# 3: A 3 4 3.934679 3.934679
# 4: B 1 2 1.766286 1.766286
# 5: B 2 3 2.925237 2.925237
# 6: B 3 4 3.966675 3.966675
The second is the closest ("Max" is correct), but what I'd like to be able to get is:
id start end Min Max
<char> <num> <int> <num> <num>
1: A 1 2 1.011845 1.858419
2: A 2 3 2.170610 2.970977
3: A 3 4 3.115194 3.934679
4: B 1 2 1.022002 1.766286
5: B 2 3 2.164325 2.925237
6: B 3 4 3.055509 3.966675
The real problem has around 400K or so rows with ranges joining in 2Mi rows of values, so I'd prefer to not do a full expansion of both frames to manually cut it back down to the size of dtA
.
(I'm open to collapse
suggestions.)