将具有范围(间隔)的data.table与具有单个值

时间:2015-11-14 10:16:24

标签: r join data.table

我有两个data.tables,一个有范围,一个有值:

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

需要根据重叠(必需结果)进行合并:

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

foverlaps {data.table}函数要求data.tables都有间隔。 findInterval {base}函数需要一个非递减断点的向量。

如何完成此合并(在data.table中)?

0 个答案:

没有答案