# 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

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.

**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.

## 15 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

pres0.1 <- rep (NA, 312)

nres0.1 = fitted0.1[j])

{pres0.1[j] <- abs(d2Lnef[j] – fitted0.1[j])

} else {nres0.1[j] <- abs(d2Lnef[j] – fitted0.1)[j]}}

pres0.1 <- na.omit(pres0.1)

nres0.1 <- na.omit(nres0.1)

I want to run this code but this error occured : missing value where TRUE/FALSE needed

How can I solve this problem?????

Hello,

The error “missing value where TRUE/FALSE needed” typically occurs when a logical test is expected (for example, in if or while statements), but the expression that’s being evaluated does not return a TRUE or FALSE value. However, your code snippet doesn’t contain any if statement or condition, so I can’t directly identify where the error comes from.

Best,

Cansu

Error in if (max(dis) > maxdis + sqrt(.Machine$double.eps)) { :

missing value where TRUE/FALSE needed

What can I do to fix this error .

I’m using vegan package doing NMDS

Hello Myrtille,

The error you’re encountering, “missing value where TRUE/FALSE needed,” typically indicates that the condition within the if statement is evaluating to NA instead of a TRUE or FALSE value. Here are a few troubleshooting steps:

to fix this.

This should return 0 if there are no missing values. If it doesn’t, then you should apply missingness handling methods.

Best,

Cansu

gfs.nhmm <- NHMM(y = obs_daily_train, X = atmvar_mme, K = 6, iters = 1000,

burnin = 100, emdist = "gamma", nmix = 1, delta = TRUE,

outdir = paste(outdir_MME, "/", sep = ""),

ypred = ypred.gfs, Xp = xp.mme)

This is a part of my NHMM code. But after running upto 8% an error is encountered

in if (sum(pro) == 0) { : missing value where TRUE/FALSE needed

I don't have missing values, NaN or Infinite values in my data.Why is this error encountered and what can be the solution?

Hello Sanghita,

The error message in if (sum(pro) == 0) { : missing value where TRUE/FALSE needed suggests that the condition being evaluated in the if statement is returning a NA (or missing value) rather than a boolean value (TRUE or FALSE).

The problem might not be directly in your observed data (obs_daily_train) or your predictor data (atmvar_mme). Rather, the error could emerge during the calculations inside the NHMM function.

Here are some steps to diagnose and possibly address the issue:

Best,

Cansu

Same problem is occurring with K=2 or 3. I had debugged NHMM code and it was called from Cgetz(z, QQ, denzity, subseqy). Print (pro) shows

NaN 2.554379e-127 NaN NaN NaN 0.00

My K(hidden states) =6. So 6 values. NaN values cannot be used by R. That’s the problem occurring. But I am not getting from where this problem occurring? Is it for small sample size? It ran successfully for a large data set.

Hello Sanghita,

I am not so familiar with the analysis. But I wouldn’t be surprised if it is about the small sample size. Please post your question on our Facebook discussion group. Someone more familiar with the concept can help there hopefully.

Best,

Cansu

The issue is persisting with K=4 also. Error called from: Cgetz(z, QQ, denzity, subseqy)

This is the pro value

5.169695e-255 6.589973e-151 NaN 0.000000e+00

NaN data in pro vector is creating problem. Sum(pro)==0 should be true. I suspect this is due to smaller dataset which might creating problem in model convergence. Training sample size of predictor is 976X600, testing sample size is only 66X600 and observed data sample size is 976X1 (for a station). But NHMM ran smoothly for a dataset like 22302X600 training sample size of predictor, 738X600 testing sample size of predictor and 22302X1 observed data training sample size. But for the smaller dataset, it is creating error. pro is computed from NHMM calculations and there I cannot make changes.