samedi 1 décembre 2018

Nested if loops in For Loop in R to compare conditions

I wanted to compare two conditions & assign a score; I have two vectors one A with some values & B is the timestamp. All I wanted to do is if say value in A is greater than a threshold & the difference in timestamps with its previous one is greater than 1 I should assign a score to C.. I am having trouble executing the following code.

for(i in 2:length(test$B)) {
  if(test$A>15 & (test$B[i]-test$B[i-1])<=4) {
    test$C<-3 
  } else if(test$A<15 & (test$B[i]-test$B[i-1]==1)) {
    test$C<-2 
  } else {
    test$C<-1
  } 
}

Aucun commentaire:

Enregistrer un commentaire