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

Henrique Dallazuanna wwwhsd at gmail.com
Mon Jan 11 22:36:45 CET 2010


Try this:

with(rle(x), lapply(lengths[values == 0], rep, x = 0))

On Mon, Jan 11, 2010 at 7:19 PM, boshao zhang <zboshao at yahoo.com> wrote:
> 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.
>
> Thanks.
>
> Bob
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list