Reanimation
Reanimation

Reputation: 3346

R - ifelse condition query

I have a test vector to which I choose weights for each, before calculating the weighted average.

I have a vector x= x which is: 0.97109891 1.02659028 0.05549137

In my function, I perform this, which assigns weights to a new vector:

       newVec <- ifelse( x < 1, 1, 
        ifelse( ((1 <= x) && (x < 2)), 0.5, 0 ) )

The newVec returns as: 1 0 1...

Which means the second condition is coming out as False and returning 0 when it should be True and returning 1. (new vector should be: 1, 0.5, 1 seems 1 <= 1.0265902)

I've tested it outside the program and the the result comes back as true... So unless I'm overseeing something I can't work out why it doesn't work in the function...

Any help would be great. Thanks in advance.

Upvotes: 0

Views: 193

Answers (1)

Christopher Louden
Christopher Louden

Reputation: 7592

&& only checks the first value of the vector. Use & to check element wise.

Upvotes: 2

Related Questions