codehitman
codehitman

Reputation: 1188

If statement in R with multiple arguments

After reading the comments section and then making the following changes, I am still getting a 'Success' instead of a 'Failure':

nrow(statedata)
[1] 100
num = -1
if(num != "best" & num != "worst" & num < 1 & num > nrow(statedata))
    print("Failure!?")
else <br />
    print("Success!")
[1] "Success!"

Upvotes: 1

Views: 4551

Answers (2)

codehitman
codehitman

Reputation: 1188

Found the solution:

if(num != "best" & num != "worst" & (num < 1 | num > nrow(statedata)))

Thanks to all! Cheers.

Logical errors are so hard to find.

Upvotes: -1

ak0053792
ak0053792

Reputation: 563

Because of the condition:

if(num != "best" | num != "worst" | num < 1 | num > length(10)) 

You should use AND not OR in negation, else the other statement will always be true. I hope you got my point here.

Upvotes: 3

Related Questions