Subsetting based on co-occurrence within a time window

人走茶凉 提交于 2019-11-30 21:20:15

问题


I am having trouble subsetting data based on different attributes in different columns. Here is a dummy data set with species, area where it was found, and time (already in POSIXct).

SP Time Area
B 07:22 1
F 09:22 4
A 09:22 1
C 08:17 3
D 09:20 1
E 06:55 4
D 09:03 1
E 09:12 2
F 09:45 1
B 09:15 1

I need to subset the rows that have SP==A, plus all other species occurring in the same area (in this case 1), within a time window of +30 and -30 minutes returning this:

SP Time Area
A 09:22 1
D 09:20 1
D 09:03 1 
F 09:45 1
B 09:15 1

I can't get past the conditional statement of this 1-hour window, should I use a for loop here, or is there a simpler way of subsetting this? Many thanks in advance.


回答1:


Reproducing just your initial result with one A value, assuming your data is called dat, can be done like so:

with(dat,dat[
  (
    SP=="A" |
    Area==Area[SP=="A"]
  ) &
  abs(difftime(Time,Time[SP=="A"],units="mins")) <= 30,
]
)

Result:

   SP                Time Area
3   A 2013-09-09 09:22:00    1
5   D 2013-09-09 09:20:00    1
7   D 2013-09-09 09:03:00    1
9   F 2013-09-09 09:45:00    1
10  B 2013-09-09 09:15:00    1

To account for multiple occurrences of A, things get a touch more complex:

with(dat,dat[
  (
    SP=="A" |
    Area %in% Area[SP=="A"]
  ) & 
  apply(
    sapply(Time[SP=="A"],
    function(x) abs(difftime(Time,x,units="mins"))<=30 ),1,any
  )
,]
)

Though I'm sure there is probably a simplification possible here somewhere.



来源:https://stackoverflow.com/questions/18689748/subsetting-based-on-co-occurrence-within-a-time-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!