Reputation: 27
In R, I have a function Outlier that accepts a numerical vector of length 3. I was trying this:
Outlier <- function(x) {
x <- sort(x)
if((x[1] < 1e-2) %% (x[1] > 1e-4))) {
print(x)
}
...
However, I was getting the error message "Error in if (condition) { : argument is not interpretable as logical". After debugging, I found that the error was being produced whenever x[1] == 0. Somehow when x[1] == 0, the logical expression evaluates to NA. With other values it works as expected. Why is this, and how can I prevent it?
Upvotes: 0
Views: 94
Reputation: 76402
To prevent this you should revise your goal. The NA
doesn't show up only when x[1]
is zero, it shows up whenever x[1] > 1e-4
evaluates to FALSE
.
TRUE %% FALSE
[1] NA
FALSE %% FALSE
[1] NA
This obviously makes sense, I was only surprised to see it not return NaN
:
1 %% 0
[1] NaN
0 %% 0
[1] NaN
Which leads me to conclude that the R
parser is clever enough to recognize the difference between logical and numerical values.
Upvotes: 2