A base R option with rle
rle
> with(rle(v),rep(seq_along(values),lengths)) [1] 1 1 2 3 3 3 4 4 5 6 6
or data.table::rleid
data.table::rleid
> data.table::rleidv(v) [1] 1 1 2 3 3 3 4 4 5 6 6
2.1m questions
2.1m answers
60 comments
57.0k users