在data.table中,查找在它们之间有其他类型事件的事件。

huangapple go评论69阅读模式
英文:

In data.table, find events that have other type of events between them

问题

我有两种类型的定时事件 - 一种具有单个坐标("points")的事件和具有“start”和“stop”属性的连续事件("lines")。对于每对连续的点,我需要查找是否有任何线段在它们之间或与它们重叠。例如:
在data.table中,查找在它们之间有其他类型事件的事件。
这里,在点BC之间没有线,而所有其他对之间都有一些线或与之重叠。

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:
在data.table中,查找在它们之间有其他类型事件的事件。
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 &lt;- fread(&#39;
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
&#39;, 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 &lt;- dt1[type == &quot;line&quot;]
dtP &lt;- dt1[type == &quot;pt&quot;]

dtP &lt;- dtP[, stop := shift(start, type = &quot;lead&quot;), subject][, fromto := paste0(id, shift(id, type = &quot;lead&quot;)), 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 := &quot;yes&quot;],
  all = T
)[is.na(result), result := &quot;no&quot;][]

#    subject fromto result
# 1:   subjA     AB    yes
# 2:   subjA     BC     no
# 3:   subjA     CD    yes
# 4:   subjA     DE    yes
# 5:   subjA     EF    yes

huangapple
  • 本文由 发表于 2023年3月1日 15:40:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定