[R] How to speed up a double loop?

jeff6868 geoffrey_klein at etu.u-bourgogne.fr
Tue Mar 3 11:49:20 CET 2015


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.



More information about the R-help mailing list