R Help – Error in if (NA) { : missing value where TRUE/FALSE needed

 

In this R tutorial you’ll learn how to deal with the error message Error in if (NA) { : missing value where TRUE/FALSE needed.

Table of contents:

Let’s start right away:

 

Basic Explanation: Missing Value Where TRUE/FALSE Needed

The error message Error in if (NA) { : missing value where TRUE/FALSE needed does always occur when the condition of an if or while loop is (partly) not existent.

Let’s illustrate that with some R code:

if(NA) {}
# Error in if (NA) { : missing value where TRUE/FALSE needed

In the previous R syntax we specified NA within the parentheses of the if statement. For that reason, the if statement was not able to decide whether the condition is TRUE or FALSE. This lead to the error message “Error in if (NA) { : missing value where TRUE/FALSE needed”.

In the following two examples, I’ll illustrate this problem based on a for-loop and based on a while-loop.

 

Example 1: Error in if Condition

Let’s assume that we have a vector with five vector elements:

vec <- 1:5

Now, let’s assume that we want to loop over this vector with a for-loop. Within this for-loop we, have an if condition, which relies on the elements of our vector.

So far so good, but now comes the problem: In the following R code we are trying to loop from 1 to 6. However, our vector has only a length of 5. For that reason, the if condition of our 6th step is missing and returns our error message:

for(i in 1:6) {
  if(vec[i] == 10) {              # This is the problematic line
    "Do something"
  }
}
# Error in if (vec[i] == 10) { : missing value where TRUE/FALSE needed

Our code works properly, if we change the first line of our syntax to the following (i.e. replacing 6 by 5):

for(i in 1:5) {

 

Example 2: Error in while Condition

The same error message can occur in while-loops. Consider the following R code:

vec <- 1:5
while(vec[6] <= 10) {             # This is the problematic line
  "Do something"
}
# Error in while (vec[6] <= 10) { : missing value where TRUE/FALSE needed

In our while-loop, we tried to access the 6th element of our vector. However, our vector has only the length 5 and therefore we receive our error message.

 

Video, Further Resources & Summary

In case you need more info on the contents of this article, you could watch the following video of my YouTube channel. In the video, I explain the R programming code of this page in RStudio.

 

The YouTube video will be added soon.

 

Furthermore, you might have a look at the other posts of my homepage.

 

In summary: In this article you learned how to get help when being faced with the error message Error in if (NA) { : missing value where TRUE/FALSE needed in the R programming language. Let me know in the comments section below, if you have additional questions.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top