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

Sean Davis sdavis2 at mail.nih.gov
Thu Jan 27 23:13:56 CET 2005


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




More information about the R-help mailing list