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:
- Basic Explanation: Missing Value Where TRUE/FALSE Needed
- Example 1: Error in if Condition
- Example 2: Error in while Condition
- Video, Further Resources & Summary
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 |
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 |
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 |
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) { |
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 |
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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
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.
6 Comments. Leave new
data.1 <- decostand(data1,"standardize",MARGIN = 1)
Error in if (any(x < 0, na.rm = na.rm)) { :
missing value where TRUE/FALSE needed
why the above error happened?
Hi Meng,
I’m sorry for the delayed response. I was on a long vacation, so unfortunately I wasn’t able to get back to you earlier. Do you still need help with your syntax?
Regards,
Joachim
I need an R code to run a split analysis. I have 10 genotypes and 2 treatments with 2 replications.
I want to make an ANOVA analysis, multiple mean comparisons for genotype, treatment, and the interactions, and possibly construct a boxplot or bar graph showing the significant difference letters on the plots.
I need assistance.
Hello Jacob,
Unfortunately, we do not have a tutorial on ANOVA yet. Maybe you can check the STHDA page for the relevant choices. Regarding the boxplots and bar plots we have an extensive series on Statistics Globe. Maybe Overlay ggplot2 Boxplot with Line in R and Draw Boxplot with Precomputed Values in R might be particularly useful in your case.
Regards,
Cansu
Hello Cansu,
Thank you very much for the information. I will try it out to see.
Best regards.
Jacob Alooh
Let me know if you need any further help. I wish you the best of luck!
Regards,
Cansu