[R] help needed to find zero areas in a vector

William Dunlap wdunlap at tibco.com
Mon Jan 11 23:00:16 CET 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of boshao zhang
> Sent: Monday, January 11, 2010 1:20 PM
> To: r-help at r-project.org
> Subject: [R] help needed to find zero areas in a vector
> 
> Dear Helpers:
>  
> I spend more than half a day to solve this problem in R:
>  
> Let x be a vector of a string of 0s and 1s, such as 
> x<-c(0,0,1,1,0,0,0,0,1,1,0,0,0,0). It can be a very long vector.
> How to sub vectors of 0s? In the above example, I would like 
> get the vectors (0,0), (0,0,0,0), (0,0,0,0). 
> I can use which(x==0) to get the index of the 0 elements, but 
> I don't know how to get the starting indices and end indices 
> of the subvectors. Maybe we should use which(x==1), but I 
> don't know how.

You can use the following functions, which return TRUE
for elements which are last (or first) in a run of equal
values and FALSE for other elements:
   lastInRun <- function(x)c(x[-1]!=x[-length(x)], TRUE)
   firstInRun <- function(x)c(TRUE, x[-1]!=x[-length(x)])
They are fast for long vectors but don't handle NA's.
E.g.,
   > x<-c(0,0,1,1,0,0,0,0,1,1,0,0,0,0)
   > data.frame(x, first=firstInRun(x), last=lastInRun(x))
      x first  last
   1  0  TRUE FALSE
   2  0 FALSE  TRUE
   3  1  TRUE FALSE
   4  1 FALSE  TRUE
   5  0  TRUE FALSE
   ...
   14 0 FALSE  TRUE
If you are only interested only in runs of 0's use
   firstInRun(x) & x==0
to flag elements of x that start a run of zeros.

For many things the logical vector output will suffice
but you can use which(logical) to convert it to the
equivalent integer indices.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

>  
> Thanks.
>  
> Bob
> 
> 
>       
> 	[[alternative HTML version deleted]]
> 
> 



More information about the R-help mailing list