Sequence of win streaks
We can use ave
and create a grouping variable with cumsum
at every occurrence of 0 in the vector and count the consecutive numbers without 0 in each group.
ave(x, cumsum(x==0), FUN = seq_along) - 1
#[1] 0 1 2 0 1 2 0 1 0 1
We can use rleid
with rowid
library(data.table)
rowid(rleid(x)) * x
#[1] 0 1 2 0 1 2 0 1 0 1
data
x <- c(0, 1, 1, 0, 1, 1, 0, 1, 0, 1)