Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

How to find the number of runs in the following

  1. aaaabbabbbaabba

  2. bbaaaaaabbbbaaaaaaa

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

2 Answers

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

share|improve this answer

Quoting http://en.wikipedia.org/wiki/Wald-Wolfowitz_runs_test:

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.

In your case

1) aaaabbabbbaabba      2) bbaaaaaabbbbaaaaaaa
   1   2 34  5 6 7         1 2     3   4
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.