# How to find the number of runs?

How to find the number of runs in the following

1. aaaabbabbbaabba

2. bbaaaaaabbbbaaaaaaa

-
added homework tag, due to original wording of question (no context, poor wording, etc) – mpiktas May 1 '11 at 14:42

If you want do this in R, check out the rle function.

For example:

> ext <- function(x) {strsplit(x, "")[[1]]}
> x <- ext("aaaabbabbbaabba")
> y <- ext("bbaaaaaabbbbaaaaaaa")
> length(rle(x)$lengths) [1] 7 > length(rle(y)$lengths)
[1] 4


See also this related question on Stack Overflow on counting runs in R

-

A "run" of a sequence is a maximal non-empty segment of the sequence consisting of adjacent equal elements. For example, the sequence "++++−−−+++−−++++++−−−−" consists of six runs, three of which consist of +'s and the others of −'s.

1) aaaabbabbbaabba      2) bbaaaaaabbbbaaaaaaa