I have a dataframe, where I am trying to find two things: 1) the start of an event and, 2) the end of the event. The start of an event is based on a cumulative sum threshold, whereas the end of an event is dependent on there being 5 rows with 0 values between the last row with a value greater than 0, and the current time.
Example data is as follows
# hourly time series
a <- seq(from=as.POSIXct("2012-06-01 0:00", tz="UTC"),
to=as.POSIXct("2012-09-01 00:00", tz="UTC"),
by="hour")
# mock data
b <- sample.int(10, 2209, replace = TRUE)*sample(c(0,1), replace=TRUE, size=2209)
# mock time series data table
c <- data.table(a,b)
a b
1: 2012-06-01 00:00:00 0
2: 2012-06-01 01:00:00 0
3: 2012-06-01 02:00:00 0
4: 2012-06-01 03:00:00 7
5: 2012-06-01 04:00:00 0
---
2205: 2012-08-31 20:00:00 8
2206: 2012-08-31 21:00:00 4
2207: 2012-08-31 22:00:00 2
2208: 2012-08-31 23:00:00 0
2209: 2012-09-01 00:00:00 0
---
I want to identify events within the time series, based on a threshold of a cumulative sum of 10 (in column b). So when a date/time has a cumulative sum of 10 or more, the event starts.
c$cumsum <- with(c, ave(b, cumsum(b == 0), FUN = cumsum))
a b cumsum
1: 2012-06-01 00:00:00 0 0
2: 2012-06-01 01:00:00 0 0
3: 2012-06-01 02:00:00 0 0
4: 2012-06-01 03:00:00 7 7
5: 2012-06-01 04:00:00 0 0
---
2205: 2012-08-31 20:00:00 8 8
2206: 2012-08-31 21:00:00 4 12
2207: 2012-08-31 22:00:00 2 14
2208: 2012-08-31 23:00:00 0 0
2209: 2012-09-01 00:00:00 0 0
For example, in the above code, an event would begin at 2012-08-31 21:00:00 due to the cumulative sum of b = 12. Also, although 2012-08-31 22:00:00 has a cumsum of 14, it is not the start of an event, as the event had begun the hour prior to it (based on the condition of event beginning when cumsum => 10).
I also need to find the end of the event, and this is where I'm stuck. The end of the event would occur when 5 hours have passed, without any values (i.e. 5 rows with 0's in column b). Then I would like to create a dataframe, which consists of only events (i.e. date/time of start of an event, with the corresponding date/time of the end of that same event). This would look like (manual, fake example):
# dataframe for event start, and the corresponding cumsum of b
event_start cumsum_b
1: 2012-06-01 00:00:00 12
2: 2012-06-09 11:00:00 11
3: 2012-06-15 02:00:00 10
# dataframe for event end
event_end b
1: 2012-06-01 00:7:00 0
2: 2012-06-09 18:00:00 0
3: 2012-06-15 12:00:00 0