Skip to content
Prev 65766 / 398513 Next

Calculating lengths of runs of 0 or 1 sequences in meteo

On 11-Mar-05 Mohammad Ehsanul Karim wrote:
The function 'rle' will do what you ask: see ?rle

For example, if X is your sequence of 0s and 1s,

  table(rle(X)$lengths)

will produce a frequency table of lengths of runs.
E.g.

  X<-sample(c(0,1),5000,replace=TRUE)
  table(rle(X)$lengths)

   1    2    3    4    5    6    7    8    9   10   11 
1181  644  333  168   83   35   15    5    3    3    3

(But -- see recent postings -- be careful about using

  hist(rle(X)$lengths)

!!!)

Hoping this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 11-Mar-05                                       Time: 11:37:50
------------------------------ XFMail ------------------------------