How can I count runs in a sequence?

RCount

R Problem Overview


In R, what would be the most efficient/simplest way to count runs of identical elements in a sequence?

For example, how to count the numbers of consecutive zeros in a sequence of non-negative integers:

x <- c(1,0,0,0,1,0,0,0,0,0,2,0,0) # should give 3,5,2

R Solutions


Solution 1 - R

Use rle():

y <- rle(c(1,0,0,0,1,0,0,0,0,0,2,0,0))
y$lengths[y$values==0]

Solution 2 - R

This can be done in an efficient way by using indexes of where the values change:

x <- c(1,0,0,0,1,2,1,0,0,1,1)

Find where the values change:

diffs <- x[-1L] != x[-length(x)]

Get the indexes, and then get the difference in subsequent indexes:

idx <- c(which(diffs), length(x))
diff(c(0, idx))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandrekosView Question on Stackoverflow
Solution 1 - RRob HyndmanView Answer on Stackoverflow
Solution 2 - RShaneView Answer on Stackoverflow