[R] Finding "runs" of TRUE in binary vector

Kjetil Brinchmann Halvorsen kjetil at acelerate.com
Fri Jan 28 11:44:11 CET 2005


Sean Davis wrote:

> I have a binary vector and I want to find all "regions" of that vector 
> that are runs of TRUE (or FALSE).
>
> > a <- rnorm(10)
> > b <- a<0.5
> > b
>  [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
>
> My function would return something like a list:
> region[[1]] 1,3
> region[[2]] 5,5
> region[[3]] 7,10
>
> Any ideas besides looping and setting start and ends directly?
>
?rle


> Thanks,
> Sean
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>
>
>


-- 

Kjetil Halvorsen.

Peace is the most effective weapon of mass construction.
               --  Mahdi Elmandjra




-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.




More information about the R-help mailing list