Calculating time lag between sequential events after grouping for subsets

蓝咒 提交于 2020-01-05 15:36:40

问题


I am trying to calculate the time between sequential observations for different combinations of my columns. I have attached a sample of my data here.

A subset of my data looks like:

head(d1) #visualize the first few lines of the data

date       time   year    km sps      pp datetime          prev  timedif   seque
<fct>      <fct> <int> <dbl> <fct> <dbl> <chr>            <dbl>    <dbl>   <chr>
2012/06/09 2:22   2012   110 MICRO     0 2012-06-09 02:22     0   260.    00
2012/06/19 2:19   2012    80 MICRO     0 2012-06-19 02:19     1  4144     01
2012/06/19 22:15  2012   110 MICRO     0 2012-06-19 22:15     0   100.    00
2012/06/21 23:23  2012    80 MUXX      1 2012-06-21 23:23     0 33855     10
2012/06/24 2:39   2012   110 MICRO     0 2012-06-24 02:39     0   120.    00
2012/06/29 2:14   2012   110 MICRO     0 2012-06-29 02:14     0    43.7   00

Where:

  • pp: which species (sps) are predators (coded as 1) and which are prey (coded as 0)
  • prev: very next pp after the current observation
  • timedif: time difference (in seconds?) between the current observation and the next one
  • seque: this is the sequence order: where the first number is the current pp and the second number is the next pp

To generate the datetime column, I did this:

d1$datetime=strftime(paste(d1$date,d1$time),'%Y-%m-%d %H:%M',usetz=FALSE) #converting the date/time into a new format

To make the other columns I used the following code:

d1 = d1 %>% 
    ungroup() %>% 
    group_by(km, year) %>% #group by km and year because I don't want time differences calculated between different years or km (i.e., locations)
    arrange(datetime)%>%
    mutate(next = dplyr::lead(pp)) %>% 
    mutate(timedif = lead(as.POSIXct(datetime))-as.numeric(as.POSIXct(datetime)))
d1 = d1[2:nrow(d1),] %>% mutate(seque = as.factor(paste0(pp,prev)))

I can then extract the average (geometric mean) time between sequences:

library(psych)
geo_avg = d1 %>% group_by(seque) %>% summarise(geometric.mean(timedif))

geo_avg 
# A tibble: 6 x 2
  seque `geometric.mean(timedif)`
  <chr>           <dbl>
1 00             58830. #prey followed by a prey
2 01            147062. #prey followed by a predator
3 0NA               NA  #prey followed by nothing (end of time series)
4 10            178361. #predator followed by prey
5 11              1820. #predator followed by predator
6 1NA               NA  #predator followed by nothing (end of time series)

I have one questions that can be broken down into three parts

  • How can I calculate the time difference between:

    1. individuals of the same sps (for example how long does it take for one MICRO to be followed by the next MICRO
    2. species-specific time for opposite classifications prey-predator (01 or 10) sequences for each prey (pp = 0) or predator (pp = 1) sps (for example, how long does it take for the prey MICRO to be followed by each other predator (pp = 1).
    3. species-specific time for same classification (00 or 11) sequences for each prey (pp = 0) or predator (pp = 1) sps (for example, how long does it take for the prey MICRO to be followed by any other prey (pp = 0), MICRO and otherwise.

I would like to be able to do something along these lines:

sps    pp    same_sps     same_class   opposite_class   
MICRO  0     10 days      5 days       2 days    
MUXX   1     15 days      20 days      12 days
etc

Just in case, here is the output for dput(d1[1:10,]):

structure(list(
 date = structure(c(11L, 21L, 21L, 23L, 26L, 31L,32L, 37L, 38L, 39L), .Label = c("2012/05/30", "2012/05/31", "2012/06/01", "2015/08/19", "2015/08/20"), class = "factor"), 
 time = structure(c(742L, 739L, 915L, 983L, 759L, 734L, 897L, 769L, 901L, 14L), .Label = c("0:00", "0:01", "0:02", "0:03", "9:58", "9:59"), class = "factor"), 
 year = c(2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L), 
 km = c(110, 80, 110, 80, 110, 110, 110, 110, 110, 110), 
 sps = structure(c(9L, 9L, 9L, 11L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("CACA", "ERDO", "FEDO", "LEAM", "LOCA", "MAAM", "MAMO", "MEME", "MICRO", "MUVI", "MUXX", "ONZI", "PRLO", "TAHU", "TAST", "URAM", "VUVU"), class = "factor"), 
 pp = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0), 
 datetime = c("2012-06-09 02:22", "2012-06-19 02:19", "2012-06-19 22:15", "2012-06-21 23:23"), 
 prev = c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0), 
 timedif = c(259.883333333333, 4144, 100.4, 43.2, 2.2, 453.083333333333), 
 seque = c("00", "01", "00", "10", "00", "00", "00", "00", "00", "00")), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), 
groups = structure(list(km = c(80, 110), year = c(2012L, 2012L), .rows = list(c(2L, 4L), c(1L, 3L, 5L, 6L, 7L, 8L, 9L, 10L))), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

回答1:


I believe you could answer all three of your questions by adding a column for the next sps and including it and the current sps in your group_by

d1 %>% 
  mutate(next_sps = lead(sps)) %>% 
  group_by(sps, next_sps, seque) %>% 
  summarise(AvgTime = mean(timedif))

# A tibble: 5 x 4
# Groups:   sps, next_sps [?]
sps   next_sps seque   AvgTime
<fct> <fct>    <chr>     <dbl>
1 MICRO MICRO    00    1.19e+  2
2 MICRO MICRO    01    4.14e+  3
3 MICRO MUXX     00    1.00e+  2
4 MICRO NA       00    1.01e-317
5 MUXX  MICRO    10    4.32e+  1


来源:https://stackoverflow.com/questions/56172464/calculating-time-lag-between-sequential-events-after-grouping-for-subsets

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