Reputation: 35
I have an issue with the all function in R.
let a
and b
two vectors:
a <- c(Inf,0)
b <- c(1,0)
When I try to evaluate the expression all(a==b)
the function returns FALSE
, is OK, if it is evaluated the expression all(a==Inf)
the function returns FALSE
, so far all is working OK, but if I try to evaluate the expression all((a==b) | (a==Inf))
the function returns TRUE
.
Could someone explain me why?
Upvotes: 2
Views: 46
Reputation: 41240
The OR
is done column wise:
a <- c(Inf,0)
b <- c(1,0)
(a==b)
#> [1] FALSE TRUE
(a==Inf)
#> [1] TRUE FALSE
(a==Inf)|(a==b)
#> [1] TRUE TRUE
In each column there's a TRUE
so each column is TRUE
Upvotes: 2
Reputation: 102251
When you type help("|")
, you will see that |
is element-wise OR
.
In this case, given
> (a == b)
[1] FALSE TRUE
> (a == Inf)
[1] TRUE FALSE
the expression (a == b) | (a == Inf)
is equivalent to
c(FALSE, TRUE) | c(TRUE, FALSE)
and the resultant logic array is c(TRUE, TRUE)
, which gives you TRUE
when you apply all
over it.
Upvotes: 2