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

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

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

问题

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

  1. require(data.table)
  2. dt1 <- fread('
  3. id subject type start stop
  4. a subjA line 11 17
  5. b subjA line 24 25
  6. c subjA line 28 30
  7. d subjA line 29 32
  8. A subjA pt 16
  9. B subjA pt 19
  10. C subjA pt 22
  11. D subjA pt 27
  12. E subjA pt 29
  13. F subjA pt 32
  14. X subjB pt 1
  15. ', 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.

  1. require(data.table)
  2. dt1 &lt;- fread(&#39;
  3. id subject type start stop
  4. a subjA line 11 17
  5. b subjA line 24 25
  6. c subjA line 28 30
  7. d subjA line 29 32
  8. A subjA pt 16
  9. B subjA pt 19
  10. C subjA pt 22
  11. D subjA pt 27
  12. E subjA pt 29
  13. F subjA pt 32
  14. X subjB pt 1
  15. &#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

  1. 可以在这里使用`foverlaps`,考虑两者都有开始和停止值,直接“lines”,但也可以考虑点,因为你对A的开始感兴趣,其中B的结束是B的开始等等。
  2. dtR <- dt1[type == "line"]
  3. dtP <- dt1[type == "pt"]
  4. dtP <- dtP[, stop := shift(start, type = "lead"), subject][, fromto := paste0(id, shift(id, type = "lead")), subject][!is.na(stop)]
  5. setkey(dtR, subject, start, stop)
  6. setkey(dtP, subject, start, stop)
  7. merge(
  8. dtP[, .(subject, fromto)],
  9. foverlaps(dtR, dtP)[, .SD[1], .(subject, fromto)][, 1:2][, result := "yes"],
  10. all = T
  11. )[is.na(result), result := "no"][]
  12. # subject fromto result
  13. # 1: subjA AB yes
  14. # 2: subjA BC no
  15. # 3: subjA CD yes
  16. # 4: subjA DE yes
  17. # 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.

  1. dtR &lt;- dt1[type == &quot;line&quot;]
  2. dtP &lt;- dt1[type == &quot;pt&quot;]
  3. dtP &lt;- dtP[, stop := shift(start, type = &quot;lead&quot;), subject][, fromto := paste0(id, shift(id, type = &quot;lead&quot;)), subject][!is.na(stop)]
  4. setkey(dtR, subject, start, stop)
  5. setkey(dtP, subject, start, stop)
  6. merge(
  7. dtP[, .(subject, fromto)],
  8. foverlaps(dtR, dtP)[, .SD[1], .(subject, fromto)][, 1:2][, result := &quot;yes&quot;],
  9. all = T
  10. )[is.na(result), result := &quot;no&quot;][]
  11. # subject fromto result
  12. # 1: subjA AB yes
  13. # 2: subjA BC no
  14. # 3: subjA CD yes
  15. # 4: subjA DE yes
  16. # 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:

确定