Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in if/while (condition) {: missing Value where TRUE/FALSE needed

Tags:

r

r-faq

People also ask

How do you solve missing value where True False needed?

In R, if the evaluation of a condition in an if or while statement results in NA, you will raise the error: missing value where TRUE/FALSE needed. You can solve this error by using is.na(x) instead of x == NA .

What does missing value where True False needed mean in R?

Encountering the occasional error message is a common part of the programming! The “missing value where true/false needed” R error results from a failure to properly define the input into an “if statement” or “while statement” as either true or false. It is an easy error to make, but also easy to correct.

How do you specify missing values in R?

In R the missing values are coded by the symbol NA . To identify missings in your dataset the function is is.na() . When you import dataset from other statistical applications the missing values might be coded with a number, for example 99 . In order to let R know that is a missing value you need to recode it.


The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.

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

This can happen accidentally as the results of calculations:

if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed

To test whether an object is missing use is.na(x) rather than x == NA.


See also the related errors:

Error in if/while (condition) { : argument is of length zero

Error in if/while (condition) : argument is not interpretable as logical

if (NULL) {}
## Error in if (NULL) { : argument is of length zero

if ("not logical") {}
## Error: argument is not interpretable as logical

if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used

I ran into this when checking on a null or empty string

if (x == NULL || x == '') {

changed it to

if (is.null(x) || x == '') {