jeudi 17 décembre 2020

Increase speed for loop operation with condition check and create object within in R

My objective is to check the condition for a cell value in a dataframe and store the return value in a cell of a new variable. I have used the code below in R to achieve the same. However, the process is very slow and takes hours to get final results. How can I modify the code below to increase processing speed?

In the code below k : Row number DF3: Dataframe

R1 <- seq(1, 2296800, 1)
for (k in seq_along(R1)){
  if(DF3[k,6]>0){
    DF3$Slack=DF3[k,6]
  } else {
    DF3$Slack=0
  }
}

Aucun commentaire:

Enregistrer un commentaire