I am a newbie, and my question is probably the easiest many of you can imagine. Still, I am stuck, and been looking for answers everywhere, so please help. I am writing a simple loop, consisting of a 'for' and an 'if' statement, evaluating a simple vector. I keep getting wrong results and I do not know where the problem is. Here is the code:
Y <- c(3,5,2,7)
for (n in Y) {
if (n < 6) (Y[n]=0)
}
When evaluated, the result is: 3 0 0 7 0, which is NOT what I am expecting! The code is supposed to evaluate every item in Y and return 0 if it is less than 6. So it should be: 0 0 0 7. What am I doing wrong? Please help!
Aucun commentaire:
Enregistrer un commentaire