Reputation: 15
I have rainfall data over a series of days. My aim is to group non-zero rainfall values into discrete events based on the gap between the Timestamp for observations.
Currently, I have a dataframe with all non-zero rainfall observations. I want to sum these observations into events. I want a new 'event'/group to be triggered if the timegap between row i and row j is greater than 30 mins. Is this possible using R:dplyr?
Upvotes: 0
Views: 52
Reputation: 12819
Maybe this could do:
library(dplyr)
df <- tibble(
time = as.POSIXct(c("2016-03-01 08:33:00", "2016-03-01 08:45:00", "2016-03-01 10:00:00", "2016-03-01 10:07:00")),
value = c(10, 12, 7, 13)
)
df %>%
mutate(timegap = difftime(time, lag(time, default = time[1])),
event = cumsum(as.numeric(timegap) > 30 * 60))
# # A tibble: 4 × 4
# time value timegap event
# <dttm> <dbl> <time> <int>
# 1 2016-03-01 08:33:00 10 0 secs 0
# 2 2016-03-01 08:45:00 12 720 secs 0
# 3 2016-03-01 10:00:00 7 4500 secs 1
# 4 2016-03-01 10:07:00 13 420 secs 1
Upvotes: 1