mardi 25 octobre 2016

Improve slow if else loop in R

I wrote a very simple code in R but it needs almost 2 hours when using it for data > 2.000.000 rows.

Is there any opportunity to improve the code? I would prefer a solution as easy as possible.

My R skills are okay (experience < 1 year) but I reached my limit in this case. Furthemore I read some articels about speeding up if else loops but I am not sure which strategy is most suitable for my code (e.g. Vectorise, ifelse, Parallelism, etc.)

Thanks for help.

    system.time(
    for (i in 1:(length(mydata$session_id)-1)){
    if (mydata$session_id[i] != mydata$session_id[i+1]){
      mydata$Einstiegskanal[i]="1"
    } else {
      mydata$Einstiegskanal[i]="0"
    }
    }
    )

    # 6877,1 Seconds = 1,91 h

Aucun commentaire:

Enregistrer un commentaire