Hi, how about this: x <- runif(100) n <- length( x )
y2 <- rep(0,n) y2[x > 0.75] <- 1 y2[x < 0.25] <- -1 cx <- cumsum(abs(y2) ) m <- match(cx, cx) y2[y2==0] <- 2 y2[x<0.5 & y2[m]==1] <- 0 y2[x>0.5 & y2[m]==-1] <- 0 y3 <- y2 y3[y3==0] <- 1 y3[y3==2] <- 0 cx <- cumsum(abs(y3)) m <- match(cx, cx) y2 <- y2[m] Best regards Bart -- View this message in context: http://www.nabble.com/How-to-get-rid-of-loop--tp23226779p23273405.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.