mardi 1 décembre 2015

Loop checks and adds to dataframe in r - not working

I am trying to loop over 10000 rows of a dataframe (df), with sequences of 24, each time performing certain checks and trying to add 1 or 0 to a new column based on those checks. The checks are to see whether the value in the next element of a column is more/less/equal compared to the current one. Overall I want to know whether there has been a move from 0 to 1, or whether there were both 1's (should result in filling an element with 1, see below).

Hope my explanation is clear, please ask if not.

ff <- function(ab){
    h <- 1
    j <- 1
    for (i in 1:ab){
        for (i in 1:24){      
            if ((j == 1) && (df$varx[h] == 1)) {      
                df$var_y <- 1
                df[h,32] <- 1
            } 
            if ((j == 1) && (ddf$varx[h] == 0)) {
                df$var_y <- 0
                df[h,32] <- 0
            }
            if ((j > 1) && (df_aa$time != 1) && (df$varx[h-1] < df$varx[h])) {
                df$var_y <- 1    
                df$var_y <- 1
                df[h+1,32] <- 1
            }
            if ((j > 1) && (df_aa$time != 1) && (df$varx[h-1] == 1) && (df$varx[h] == 1)) {
                df$var_y <- 1
                df[h,32] <- 1
            }
            if ((j > 1) && (df_aa$time != 1) && (df$varxh-1] == 1) && (df$varx[h] == 0)) {     
                df$var_y<- 0
                df[h,32] <- 0
            } 
            h <- h + 1
            }
            j <- 1
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire