I am currently using R. I have a large table of data with an hourly time stamp, and an observation for each hour. I need to group all observations > 0 that occur within 4 hours of each other as a single event. Example data is below:
Date Obs
2017-12-01 5 0.01
2017-12-01 6 0.5
2017-12-01 7 0.2
2017-12-01 8 0
2017-12-01 9 0.03
2017-12-01 10 0.01
2017-12-01 11 0
2017-12-01 12 0
2017-12-01 13 0
2017-12-01 14 0
2017-12-01 15 0
2017-12-01 16 0
2017-12-01 17 0
2017-12-01 18 1.2
2017-12-01 19 0.6
For instance the first six rows would be a single event (0.01, 0.5, 0.2, 0. 0.03, 0.01), since there is only one hour of a non observation (a zero). Then the consecutive rows of 4 zeros or more would trigger a non-event. Event 2 would be started next time we have a positive reading (1.2, 0.6) etc.
I have attempted to do this using the rle() function. For example:
events <- rle(data$Obs > 0)
However, this creates a non-event for every 0. Is there a simple solution to this? Thanks.