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

skan juanpide at gmail.com
Sat Sep 11 12:54:14 CEST 2010


OK

thanks to all, I don't need, more
Up to this moment the fastest working code is Jonathan Chang's
http://stackoverflow.com/questions/3686982/r-adding-zeroes-after-old-zeroes-in-a-vector/3689360#3689360

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-tp2534824p2535573.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list