与开始和结束位置重叠连接
data.table
library(data.table)d1 <- data.table(x = letters[1:5], start = c(1,5,19,30, 7), end = c(3,11,22,39,25))setkey(d1, x, start) # x start end# 1: a 1 3# 2: b 5 11# 3: c 19 22# 4: d 30 39# 5: e 7 25
d2 <- data.table(x = letters[c(1,1,2,2,3:5)], pos = c(2,3,3,12,20,52,10))setkey(d2, x, pos)# x pos# 1: a 2# 2: a 3 # 3: b 3# 4: b 12# 5: c 20# 6: d 52# 7: e 10
x
# x pos start end# 1: a 2 1 3# 2: a 3 1 3# 3: c 20 19 22# 4: e 10 7 25
x
d1[d2, roll = TRUE, nomatch = 0, mult = "all"][start <= end]
# x start end# 1: a 2 3# 2: a 3 3# 3: c 20 22# 4: e 10 25