

Improve Article
Save Article
In this article, we are going to see how to fix missing values where true/false is needed in R Programming Language.
Why Does This Error Occur?
When we will compare the value with NA(Not available) putting into an “if statement” or “while statement”, then the ”missing value where true/false needed” error occurs is that you are passing an invalid value to “if statement” or “while statement”.
Example 1: Fixing with variables
How to Reproduce the Error
Here we will get the error because we used the syntax num == NA. We can not compare the value with NA number, this type of statement(If condition) always returns TRUE or FALSE value.
R
|
Output:
Error in if (num == NA) {: missing value where TRUE/FALSE needed
How to Fix the Error
We need to use is.na(Value) because it always returns TRUE or FALSE.
R
|
Output:
[1] "2"
Example 2: Fixing with vector
Here we will get the error because we used the syntax vec[l] != NA. We can not compare the value with NA number, this type of statement(If condition) always returns TRUE or FALSE value.
R
|
Output:
Error in if (num == NA) {: missing value where TRUE/FALSE needed
You have to use the function is.na for your if statement to work.
R
|
Output:
[1] 1 [1] 0