1

I have a sequence of 0s and 1s,

test = c(0,0,0,1,1,0,1,0,0)

An event is defined by the first 0 in a subsequence of 0s. I am interested in the number of zeros (count) in each event.

For test above, we would have:

  • Event 1: 0 0 0
  • Event 2: 0
  • Event 3: 0 0

So, I would like to create the following table,

Event count
1 3
2 1
3 2

I have started creating a function with if clauses. But it became too heavy to run with the long vector that I am using.

Then I started a new code with dplyr, but I can't get it to work it properly.

What would be the most efficient way of doing this?

Here's my sample (and incorrect) code:

library(dplyr)
test = c(0,0,0,1,1,0,1,0,0)
df = data.frame(test)
df$cumsum = cumsum(df$test==0L)

df%>%
  group_by(test, cumsum)%>%
  mutate(counter=row_number())%>%
  ungroup 
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Michel Mesquita
  • 743
  • 7
  • 17

0 Answers0