FinThusiast
FinThusiast

Reputation: 149

Understanding 'and' and equal to operators in R

In R, TRUE && factor(FALSE) gives an error but TRUE && factor(FALSE) == FALSE returns TRUE. When TRUE && factor(FALSE) cannot be computed then how does R compares it with FALSE?

Also FALSE && factor(FALSE) returns FALSE but FALSE && factor(FALSE) == FALSE returns FALSE, it should return TRUE because the left hand side expression evaluates to FALSE. I tried FALSE && factor(FALSE) == TRUE but that also returns FALSE. Can someone explain the above results?

Upvotes: 1

Views: 52

Answers (1)

Saurabh Chauhan
Saurabh Chauhan

Reputation: 3221

Kindly look at the operator precedence. As per the list == as highest precedence then && so the FALSE && factor(FALSE) == FALSEreturns FALSE as it first evaluates == and &&. If you want to execute && first and then == then use the proper bracket:

(FALSE && factor(FALSE)) == FALSE

And it returns TRUE. If you execute:

FALSE & factor(FALSE) == FALSE

Then first it executes factor(FALSE) == FALSE which executes to TRUE and then FALSE && TRUE so finally you will get FALSE.

Upvotes: 2

Related Questions