[R] Calculating lengths of runs of 0 or 1 sequences in meteo
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Fri Mar 11 12:37:50 CET 2005
On 11-Mar-05 Mohammad Ehsanul Karim wrote:
> Dear List Members,
>
> I need some help about programming in S language. My
> problem is as follows:
>
> I have meteorological data (about rainfall measurement
> each day from 1989-2002), say like
> http://www.angelfire.com/ab5/get5/data.rainfall.txt
> or http://www.angelfire.com/ab5/get5/R.rainfall.txt
> in a sequence of 0(denoting dry day)'s and 1(denoting
> wet day)'s. I want to construct a frequency
> distribution table of various lengths
> (1,2,3,4,5,6,7,8,9,or more) of observed wet spells
> (number of successive 1's) and dry spells (number of
> successive 0's) occurring in data.
>
> How should i proceed? Is there any existing
> program/function/package to solve such problem (seems
> like the algorithm should be similar to statistical
> run test)?
>
> Any suggestion, direction, references, help, replies
> will be highly appreciated.
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 ------------------------------
More information about the R-help
mailing list