Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in R Programming by (7.3k points)

I received this error message:

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

or

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

What does it mean, and how do I prevent it?

2 Answers

+2 votes
by
edited by

The possible reasons for getting this error are as follows:

  • When the if/while condition evaluates to NA.

For example:

> if(NA){}

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

> while(NA){}

Error in while (NA) { : missing value where TRUE/FALSE needed

  • When the argument passed is of length Zero.i.e.,

> if(NULL){}

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

  • When the condition resulted in something that could not be interpreted as logical.

For example:

 if("condition") {}

Error in if ("condition") { : argument is not interpretable as logical

  • When multiple values are passed in the condition.i.e.,

if (c(TRUE, FALSE)) {}

NULL

Warning message:

In if (c(TRUE, FALSE)) { :

  the condition has length > 1 and only the first element will be used

If you want to explore more in R programming then watch this R programming tutorial for beginners:

+1 vote
by (32.3k points)
edited by

I once ran into a similar problem while I was checking on a null or empty string:

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

Then, I changed it to something like this:

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

And the problem was solved.

Browse Categories

...