Search code examples
rtimedurationposixctthreshold

Adding dates and times to event durations


As an addition to this question, is it possible to add when an event started and when it finished in another column(s)?

Here is a reproducible example pulled from the OP.

df <- structure(list(Time = structure(c(1463911500, 1463911800, 1463912100, 
1463912400, 1463912700, 1463913000), class = c("POSIXct", "POSIXt"
), tzone = ""), Temp = c(20.043, 20.234, 6.329, 20.424, 20.615, 
20.805)), row.names = c(NA, -6L), class = "data.frame")

> df
                 Time   Temp
1 2016-05-22 12:05:00 20.043
2 2016-05-22 12:10:00 20.234
3 2016-05-22 12:15:00  6.329
4 2016-05-22 12:20:00 20.424
5 2016-05-22 12:25:00 20.615
6 2016-05-22 12:30:00 20.805

library(dplyr)
df %>% 
  # add id for different periods/events
  mutate(tmp_Temp = Temp > 20, id = rleid(tmp_Temp)) %>% 
  # keep only periods with high temperature
  filter(tmp_Temp) %>%
  # for each period/event, get its duration
  group_by(id) %>%
  summarise(event_duration = difftime(last(Time), first(Time)))


     id event_duration
  <int> <time>        
1     1  5 mins       
2     3 10 mins 

i.e there are two more columns: "start_DateTime" and "end_DateTime"

Thanks!


Solution

  • Sure. Modify the final summarise() like this:

    df %>% 
      # add id for different periods/events
      mutate(tmp_Temp = Temp > 20, id = rleid(tmp_Temp)) %>% 
      # keep only periods with high temperature
      filter(tmp_Temp) %>%
      # for each period/event, get its duration
      group_by(id) %>%
      summarise(event_duration = difftime(last(Time), first(Time)),
                start_DateTime = min(Time),
                end_DateTime = max(Time))
    
    #> # A tibble: 2 × 4
    #>      id event_duration start_DateTime      end_DateTime       
    #>   <int> <drtn>         <dttm>              <dttm>             
    #> 1     1  5 mins        2016-05-22 12:05:00 2016-05-22 12:10:00
    #> 2     3 10 mins        2016-05-22 12:20:00 2016-05-22 12:30:00