mardi 11 octobre 2016

Nested if statements - R

I am having a problem with multiple if statements. The output is always taken from the first condition, even if the condition is not correct.. Can anybody point out what am I doing wrong?

code:

for(i in 1:length(data_model3$Gewicht)){
    if (data_model3$Gewicht[i] < 1){
      data_model3$LIXshipping <- 3.26 
    }
    else if (data_model3$Gewicht[i] > 1 & data_model3$Gewicht[i] < 2){
      data_model3$LIXshipping <- 3.26 
    }
    else if (data_model3$Gewicht[i] > 2 & data_model3$Gewicht[i] < 3){
      data_model3$LIXshipping <- 3.86 
    }
    else if (data_model3$Gewicht[i] > 3 & data_model3$Gewicht[i] < 4){
      data_model3$LIXshipping <- 3.86 
    }
    else if (data_model3$Gewicht[i] > 4 & data_model3$Gewicht[i] < 5){
      data_model3$LIXshipping <- 3.86 
    }
    else if (data_model3$Gewicht[i] > 5 & data_model3$Gewicht[i] < 10){
      data_model3$LIXshipping <- 4.39 
    }
    else if (data_model3$Gewicht[i] > 10 & data_model3$Gewicht[i] < 20){
      data_model3$LIXshipping <- 4.81 
    }
    else if (data_model3$Gewicht[i] > 20 & data_model3$Gewicht[i] < 31.5){
      data_model3$LIXshipping <- 6.34 
    }
    else 
      data_model3$LIXshipping <- 17.09 
}

output:

count_code  Gewicht LIXshipping
281         4.67    3.26
683         3.34    3.26
903         14.56   3.26
7390        14.62   3.26
2778        2.86    3.26
2778        10.37   3.26

It might be a compleately basic question for some of you.. but I have no idea how to fix it.
Andraz

Aucun commentaire:

Enregistrer un commentaire