3 == 7|5
I know R uses "==" in precedence of "||, but what does the code above actually mean and why it gives a TRUE in return
CodePudding user response:
Any value other than 0 is considered as TRUE
FALSE|5
[1] TRUE
If we do all the precautionary measures i.e. check for precedence by wrapping within ()
> (3 == 7)|5
[1] TRUE
because 3 == 7
returns FALSE and by default TRUE/FALSE
values coerce to 1/0
. Also, check the as.logical
coersion
> as.logical(c(5, 0))
[1] TRUE FALSE
CodePudding user response:
Explanation:
|
is a logical or. Which gives TRUE
if the boolean representative is TRUE
from both expressions, with the order of operations in R, your code get's processed as:
(3 == 7) | 5
Whereas 3 == 7
gives FALSE
, but the boolean representative of 5
is TRUE
, so FALSE | TRUE
gives TRUE
.
Graph:
Here is a graph (tree) of the way this code gets processed:
(3 == 7) | 5
↙ ↘
FALSE | TRUE
↓↓↓
TRUE