1

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
Jaap
  • 81,064
  • 34
  • 182
  • 193
lp1585
  • 45
  • 3
  • 3
    `aggregate(Event ~ Year, data = mydf, function(x) with(rle(x), lengths[!values]))`? – Jaap Feb 09 '16 at 13:42
  • A [good read on grouping functions](http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega) – Jaap Feb 09 '16 at 14:01

2 Answers2

1

Based on your previous answer you can likely just use the tapply function. The answer will probably return a list

tapply(data$Event,list(data$Year),
   FUN=function(x) with(rle(x[Reduce(':',as.list(range(which(x==1))))]),lengths[!values]))

or if you've already done your data manipulations

    tapply(data$Event,list(data$Year), FUN=function(x) with(rle(x),lengths[!values]))
s_scolary
  • 1,361
  • 10
  • 21
1

We can use data.table

library(data.table)
setDT(df1)[,{x1 <- Event[Reduce(':',as.list(range(which(Event==1))))]
  with(rle(x1), lengths[!values])  }, Year]
#   Year V1
#1: 1916  3
#2: 1916  3
#3: 1917  1
akrun
  • 874,273
  • 37
  • 540
  • 662