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.
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?
Hi: Try the following: f <- function(x, n) { r <- rle(x) t1 <- cumsum(r$lengths)[r$values == 0L] + 1 repl <- as.vector(mapply(seq, t1, t1 + n - 1)) replace(x, repl, 0) } f(x, 3) HTH, Dennis On Fri, Sep 10, 2010 at 10:51 AM, skan <juanpide@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@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. >[[alternative HTML version deleted]]
Hello, Dennis Do you prefer your way or this one? http://stackoverflow.com/questions/3686982/r-adding-zeroes-after-old-zeroes-in-a-vector stackoverflow, Jonathan <quote rr <- rle(tmp) ## Pad so that it always begins with 1 and ends with 1 if (rr$values[1] == 0) { rr$values <- c(1, rr$values) rr$lengths <- c(0, rr$lengths) } if (rr$values[length(rr$values)] == 0) { rr$values <- c(rr$values, 1) rr$lengths <- c(rr$lengths, 0) } zero.indices <- seq(from=2, to=length(rr$values), by=2) one.indices <- seq(from=3, to=length(rr$values), by=2) rr$lengths[zero.indices] <- rr$lengths[zero.indices] + pmin(rr$lengths[one.indices], n) rr$lengths[one.indices] <- pmax(0, rr$lengths[one.indices] - n) inverse.rle(rr) -- View this message in context: http://r.789695.n4.nabble.com/adding-zeroes-after-old-zeroes-in-a-vector-tp2534824p2534995.html Sent from the R help mailing list archive at Nabble.com.
Hello Your code gives a vector with length different to the original one -- View this message in context: http://r.789695.n4.nabble.com/adding-zeroes-after-old-zeroes-in-a-vector-tp2534824p2535017.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2010-Sep-10 20:13 UTC
[R] adding zeroes after old zeroes in a vector ??
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? >Here is a solution using rollapply.zoo. It makes use of the development version of zoo in which the rollapply function supports an additional partial= argument. It is based on the idea that if any subsequence starts with 0, ends with 1 and is nondecreasing then its last element must be replaced with 0. library(zoo) # grab development version of rollapply source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/rollapply.R?revision=799&root=zoo") # test data z <- zoo(c(1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1)) N <- 3 f <- function(x, xn = tail(x, 1)) if (!x[1] && xn && all(diff(x) >0)) 0 else xn rollapply(z, N+1, FUN = f, align = "right", partial = TRUE) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Here are a couple of options:> tmp <- strsplit('1111111100001111111111110000000001111111111100101','') > tmp <- as.numeric(unlist(tmp)) > > n <- 3 > > tmp2 <- embed(tmp, n+1) > > tmp3 <- tmp > tmp3[ which( apply( tmp2, 1, function(x) any(x==0) ) ) + n ] <- 0 > > paste(tmp3, collapse='')[1] "1111111100000001111111110000000000001111111100000"> > > > library(gtools) > > tmpfun <- function(x) {+ if(any(x==0)) { + 0 + } else { + x[length(x)] + } + }> > tmp4 <- running( tmp, width=4, fun=tmpfun,+ allow.fewer=TRUE )> > tmp4 <- unlist(tmp4) > paste(tmp4, collapse='')[1] "1111111100000001111111110000000000001111111100000" -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of skan > Sent: Friday, September 10, 2010 11:52 AM > To: r-help at r-project.org > Subject: [R] adding zeroes after old zeroes in a vector ?? > > > 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.