I have some precipitation data that I converted into binary, where 1 = a precipitation event and 0 = no precipitation. The data set has over 35,000 values, but here is an example of what my data would look like this:
x = c(1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1)
I would like to count the number of consecutive days without an event, so my output would look like this:
y = 2, 3, 6, 2.
I was given the following answer, which worked great:
with(rle(x), length[!values])
#[1] 2 3 6 2
If we have '0's at the end of the vector, we can start the count from the first 1 to the last 1
x1 <- x[Reduce(':',as.list(range(which(x==1))))]
with(rle(x1), lengths[!values])
Now, my question is: is there a way to do this while grouping the data by year? Here is a sample from my data set:
Event Year
1 1916
1 1916
0 1916
0 1916
0 1916
1 1916
0 1916
0 1916
0 1916
1 1916
0 1917
0 1917
0 1917
0 1917
0 1917
1 1917
0 1917
1 1917
1 1917
1 1917