英文:
In data.table, find events that have other type of events between them
问题
我有两种类型的定时事件 - 一种具有单个坐标("points")的事件和具有“start”和“stop”属性的连续事件("lines")。对于每对连续的点,我需要查找是否有任何线段在它们之间或与它们重叠。例如:
这里,在点B
和C
之间没有线,而所有其他对之间都有一些线或与之重叠。
require(data.table)
dt1 <- fread('
id subject type start stop
a subjA line 11 17
b subjA line 24 25
c subjA line 28 30
d subjA line 29 32
A subjA pt 16
B subjA pt 19
C subjA pt 22
D subjA pt 27
E subjA pt 29
F subjA pt 32
X subjB pt 1
', fill=T)
我正在尝试使用foverlaps()
函数,并可以标记与线段重叠的单个点,但这不是我所需要的 - 我需要找到位于两个点之间的线段。
英文:
I have two types of timed events - those with a single coordinate ("points") and continuous ones ("lines") having start
and stop
. For each pair of consecutive points, I need to find if there is any line between or overlaping with them. For example:
Here, there is no lines between points B
and C
while all other pairs have some lines between them or overlapping with them.
require(data.table)
dt1 <- fread('
id subject type start stop
a subjA line 11 17
b subjA line 24 25
c subjA line 28 30
d subjA line 29 32
A subjA pt 16
B subjA pt 19
C subjA pt 22
D subjA pt 27
E subjA pt 29
F subjA pt 32
X subjB pt 1
', fill=T)
I am trying to play with foverlaps()
and can mark single points that overlap with lines, but that's not what I need - to find lines between points.
答案1
得分: 0
可以在这里使用`foverlaps`,考虑两者都有开始和停止值,直接“lines”,但也可以考虑点,因为你对A的开始感兴趣,其中B的结束是B的开始等等。
dtR <- dt1[type == "line"]
dtP <- dt1[type == "pt"]
dtP <- dtP[, stop := shift(start, type = "lead"), subject][, fromto := paste0(id, shift(id, type = "lead")), subject][!is.na(stop)]
setkey(dtR, subject, start, stop)
setkey(dtP, subject, start, stop)
merge(
dtP[, .(subject, fromto)],
foverlaps(dtR, dtP)[, .SD[1], .(subject, fromto)][, 1:2][, result := "yes"],
all = T
)[is.na(result), result := "no"][]
# subject fromto result
# 1: subjA AB yes
# 2: subjA BC no
# 3: subjA CD yes
# 4: subjA DE yes
# 5: subjA EF yes
英文:
You can use foverlaps
here and consider both have a start and stop value, "lines" directly, but the points also as you are interested in the start of A, where the end is the start of B, etc.
dtR <- dt1[type == "line"]
dtP <- dt1[type == "pt"]
dtP <- dtP[, stop := shift(start, type = "lead"), subject][, fromto := paste0(id, shift(id, type = "lead")), subject][!is.na(stop)]
setkey(dtR, subject, start, stop)
setkey(dtP, subject, start, stop)
merge(
dtP[, .(subject, fromto)],
foverlaps(dtR, dtP)[, .SD[1], .(subject, fromto)][, 1:2][, result := "yes"],
all = T
)[is.na(result), result := "no"][]
# subject fromto result
# 1: subjA AB yes
# 2: subjA BC no
# 3: subjA CD yes
# 4: subjA DE yes
# 5: subjA EF yes
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论