vendredi 6 avril 2018

R condition loop. Error takes only the first element

I am using this double condition to get an output in a new column, but it gives me only the first match and not all that match the conditions. In the third and fourth line in the column "scores2" hast to be 0,2 and not 0. It would be great if someone can help me. Thanks a lot in advance.

id  touchpoint_ts   tp_type Referrer    scores  scores2
1   31.10.2017 05:09    a   4   1,7 0
1   13.10.2017 03:19    b   2   0,1 0,2
1   12.10.2017 06:19    b   2   0,1 0
1   12.10.2017 06:19    b   2   0,1 0
1   12.10.2017 06:19    b   0   0,1 0
1   08.09.2017 14:26    a   3   3   0

dataset$scores2<-c(0)
for (i in 1:length(dataset$id)){
  if (dataset$tp_type[i] == "b"){
    while ((dataset$Referrer[i]) > 1){
      dataset$scores2[i]=dataset$scores[i]*2
    } } else {
      dataset$scores2[i]=0
    }
}

Aucun commentaire:

Enregistrer un commentaire