I have two data.tables, one with a range and one with a value:
x = data.table(start = c(5,31,22,16), end = c(8,50,25,18))
y = data.table(val = c(40, 17, 7, 23))
x
# start end
#1: 5 8
#2: 31 50
#3: 22 25
#4: 16 18
y
# val
#1: 40
#2: 17
#3: 7
#4: 23
The need to be merged based on the overlaps (required result):
res = data.table(start = c(5,31,22,16), end = c(8,50,25,18), val = c(7, 40, 23, 17))
res
# start end val
#1: 5 8 7
#2: 31 50 40
#3: 22 25 23
#4: 16 18 17
The foverlaps
{data.table} function requires both data.tables to have an interval. The findInterval
{base} function requires a vector of non-decreasing breakpoints.
How can this merge be done (in data.table) ?