- A+

Category：Languages

If I have a vector like

`"a": 0 0 1 1 1 0 0 0 0 1 1 0 0 0 `

How can I generate a vector of the same length containing the count of consecutive elements, like so:

`"b": 2 2 3 3 3 4 4 4 4 2 2 3 3 3 `

I tried rle, but I did not manage to stretch it out this way.

Another option using `rle`

and `rep`

`with(rle(a), rep(lengths, times = lengths)) # [1] 2 2 3 3 3 4 4 4 4 2 2 3 3 3 `

**data**

`a <- c(0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0) `