jeudi 5 avril 2018

R if condition with dates in a loop

I want to extract a result with a double condition (one of them is with dates) using a loop. If touchpoint_type is equal to "b" and datum [i] is previous to datum[i-1] then multiply by 2 the indiv_scores[i] and paste it in indiv_scores2[i]. I attache the code. Apparently there is a problem with "<".

id  datum   touchpoint_type indiv_scores    indiv_scores2
1   31.10.2017 05:09    a   1,7 1,7
2   13.10.2017 03:19    b   0,1 0,2
3   10.09.2017 14:26    a   3   3


for (i in 1:length(dataset$id)){
  if (dataset$touchpoint_type[i] == "b"){
    while (as.Date(dataset$datum[i])) < (as.Date(dataset$datum[i-1]))
    {
    dataset$indiv_scores2[i]=dataset$indiv_scores[i]*2
  } } else {
    dataset$indiv_scores2[i]=dataset$indiv_scores[i]
  }
}

Thank you very much in advance

Javi

Aucun commentaire:

Enregistrer un commentaire