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

james.holtman@convergys.com james.holtman at convergys.com
Fri Jan 28 03:28:32 CET 2005





use 'rle';

> a <- rnorm(20)
> b <- a < .5
> b
 [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE
TRUE
[13] FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
> rle(b)
Run Length Encoding
  lengths: int [1:9] 1 7 2 2 2 3 1 1 1
  values : logi [1:9] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
>
__________________________________________________________
James Holtman        "What is the problem you are trying to solve?"
Executive Technical Consultant  --  Office of Technology, Convergys
james.holtman at convergys.com
+1 (513) 723-2929


                                                                                                                                           
                      Sean Davis                                                                                                           
                      <sdavis2 at mail.nih.gov        To:       r-help <r-help at stat.math.ethz.ch>                                             
                      >                            cc:                                                                                     
                      Sent by:                     Subject:  [R] Finding "runs" of TRUE in binary vector                                   
                      r-help-bounces at stat.m                                                                                                
                      ath.ethz.ch                                                                                                          
                                                                                                                                           
                                                                                                                                           
                      01/27/2005 17:13                                                                                                     
                                                                                                                                           
                                                                                                                                           




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?

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




More information about the R-help mailing list