[R] adding zeroes after old zeroes in a vector ??

jim holtman jholtman at gmail.com
Fri Sep 10 20:59:17 CEST 2010


Not sure how you handle the ending sequence of '0101';  here is one approach:

> x <- "1111111100001111111111110000000001111111111100101"
> gsub("0111", "0000", x)
[1] "1111111100000001111111110000000000001111111100101"
> x
[1] "1111111100001111111111110000000001111111111100101"
>

For the final one, you could do:

> gsub("01..", "0000", x)
[1] "1111111100000001111111110000000000001111111100000"


On Fri, Sep 10, 2010 at 1:51 PM, skan <juanpide at gmail.com> wrote:
>
> Hello
>
> Imagine I have a vector with ones and zeroes
>
> I write it compactly:
> 1111111100001111111111110000000001111111111100101
>
> I need to get a new vector replacing the "N" ones following the zeroes to
> new zeroes.
>
> For example for N = 3
> 1111111100001111111111110000000001111111111100101  becomes
> 1111111100000001111111110000000000001111111100000
>
> I can do it with a for loop but I've read is not a good practice,  How can I
> do it then?
>
> cheers
>
>
> My vector is a zoo series, indeed, but I guess it doesn't make any
> difference.
> --
> View this message in context: http://r.789695.n4.nabble.com/adding-zeroes-after-old-zeroes-in-a-vector-tp2534824p2534824.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list