3

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) ?

Henrik
  • 65,555
  • 14
  • 143
  • 159
Henk
  • 3,634
  • 5
  • 28
  • 54
  • 2
    Start by creating an 'interval' of the point as described [here](http://stackoverflow.com/questions/24480031/roll-join-with-start-end-window/25655497#25655497) (i.e. the `pos2 := pos` step) – Henrik Nov 14 '15 at 10:24
  • Thanks Henrik. That solves it. I didn't discover this question, so my question can be closed as a duplicate. – Henk Nov 14 '15 at 10:55

0 Answers0