[R] identify number of sequences from a vector
Marc Schwartz
marc_schwartz at comcast.net
Tue Oct 9 16:41:51 CEST 2007
On Tue, 2007-10-09 at 16:28 +0200, Niccolò Bassani wrote:
> dear R users, I've got kind of strange problem that I can't solve, even
> though it's not that difficult. I need to compute the wald-wolfowitz run
> test on the residuals from a gee model, and I'm trying to build the
> quantities I need by hand. Whereas for the expected and the variance there's
> no problem, I do not know how to quantify the number of runs, i.e. the
> number of sequences of equal values.
> With a short vector like 1 1 1 0 0 1 1 1 1 1 1 it is easy to see 3
> sequences, but my residuals are more than 7000, so it's quite difficult to
> trust on eyes only. I'm looking for a way to get this one, but I can't
> figure out.
> Moreover, i found the wawotest function in the adehabitat package doing this
> test, but the results (which is mean, variance...) are different from what I
> got by calculatin them by hand so I wanted to finish my calculations to
> better understand what's going on.
> Any help will be appreciated. Just give me a hint, and I'll do my best to
> rule this out...
> thanks
> niccol
Look at ?rle
Example:
> Vec
[1] 1 1 1 0 0 1 1 1 1 1 1
> rle(Vec)
Run Length Encoding
lengths: int [1:3] 3 2 6
values : num [1:3] 1 0 1
> length(rle(Vec)$lengths)
[1] 3
HTH,
Marc Schwartz
More information about the R-help
mailing list