I tried another faster way which seems to do the trick right now: myts <-data.frame(x=c(10,2,50,40,NA,NA,0,50,1,2,0,0,NA,50,0,15,3,5,4,20,0,0,25,22,0,1,100),z=NA) test <- function(x){ st1 <- numeric(length(x)) temp <- st1[1] for (i in 2:(length(x))){ if((!is.na(x[i])) & (!is.na(x[i-1]))& (abs((x[i])-(temp)) >= 15)){ st1[i] <- 1 } } return(st1) }
myts[,2] <- apply(as.data.frame(myts[,1]),2,test) myts[,2] <- as.numeric(myts[,2]) Thanks anyway for your help! -- View this message in context: http://r.789695.n4.nabble.com/How-to-speed-up-a-double-loop-tp4704054p4704112.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.