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
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