I'm trying to understand the |
operator in R. Why does
a = 2
a == 3 | 4
return TRUE
in R?
a == 3
and
a == 4
each return FALSE
so why does the second line return TRUE
?
See help(Syntax)
-- the ==
has higher precedence than the |
.
So:
R> a <- 2
R> a == 3 | 4
R> TRUE
R> a == (3 | 4)
R> FALSE
Think of it like this:
`|`(a == 3, 4)
`==`(a, 3)
as.logical(2) # TRUE
as.logical(3) # TRUE
as.logical(4) # TRUE
So, what is happening is that both sides of a == 3
are coerced to logical; that evaluates to TRUE == TRUE
which is TRUE
. After that an or
operation between TRUE
and 4
returns TRUE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With