[R] times 'til first change

Philippe Grosjean phgrosje at ulb.ac.be
Mon Oct 29 23:16:01 CET 2001


Here is just a starting point:

> a <- c(0,0,1,0,1,1,1,1,0,1)
> unlist(sapply(rle(a)$lengths, function(x) x:1))
 [1] 2 1 1 1 4 3 2 1 1 1

To exactly match your question, you still have to drop the last element and
program a special treatment for NAs.
Best,

Philippe Grosjean


...........]<(({?<...............<?}))><...............................
 ) ) ) ) )	 __               	 __
( ( ( ( ( 	|__)              	|  _
 ) ) ) ) )	|   hilippe       	|__)rosjean
( ( ( ( ( 	Marine Biol. Lab., ULB, Belgium
 ) ) ) ) )	                  	 __
( ( ( ( ( 	|\  /|            	|__)
 ) ) ) ) )	| \/ |ariculture &	|__)iostatistics
( ( ( ( (
 ) ) ) ) )	e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( 	SciViews project coordinator (http://www.sciviews.org)
 ) ) ) ) )      tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
 ) ) ) ) )      "I'm 100% confident that p is between 0 and 1"
( ( ( ( (                                  L. Gonick & W. Smith (1993)
 ) ) ) ) )
......................................................................



-----Message d'origine-----
De : owner-r-help at stat.math.ethz.ch
[mailto:owner-r-help at stat.math.ethz.ch]De la part de Claudia Tebaldi
Envoye : lundi 29 octobre 2001 20:30
A : r-help at stat.math.ethz.ch
Objet : [R] times 'til first change


There is probably an elegant and efficient way to do this -- perhaps
already implemented, and some of you knows it. Myself, I'm getting stuck
in long loops ...

Here is the question:

I have a long (80,000 + obs) binary time series.
For each of its elements I'd like to compute and save in a vector "the
number of steps 'til the first switch of state happens". For example, say
the first 10 obs of my x series look like

                 0 0 1 0 1 1 1 1 0 1

I want to come up with a 9-component y vector that looks like

                 2 1 1 1 4 3 2 1 1

(it took two steps for the state to change from 0 to 1 if I take the first
obs as the state of reference, one step if i take the second obs as
reference ... and so on).
I don't care about distinguishing changes from 0 to 1 from their opposite,
but I'd like to have NAs appear in the derived series in case NAs appeared
in the original time series BEFORE a switch, say

             x    0 0 1 0 1 1 1 NA 0 1.....

should originate

             y    2 1 1 1 NA NA NA NA 1....

because for the series of 1's starting in the 5th position I really don't
know if the switch happened at the 8th or at the 9th step.

thanks in advance

claudia


-------------------------------------------------------------------------
claudia tebaldi                                  NCAR  RAP
project scientist                                P.O. Box 3000
(303) 497-2830                                   Boulder, CO 80307
--------------------------------------------------------------------------





-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list