using the if function

I'm currently working through Programming with R, and I can't seem to make my "if" function work on a vector with length > 1. The book says to use the "any" function as "if" has to evaluate a single TRUE or FALSE.

I have written:

num<-c(-2,-1,0,1,2)
if(any(num<0)){num<-num*-1}
num
[1] 2 1 0 -1 -2

Can anyone help point out my mistake?

Thanks!

it worked.
any(num<0)
is true

so num signs got flipped.
the first value that was -2 is now 2

Thanks so much for such a quick reply! I wasn't using my brain, apologies. I thought it should return 2,1,0,1,2 but I see now why that's not the case. Thanks :blush:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.