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

569    Asked by ArjunArora in Python , Asked on Apr 5, 2021

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?

Answered by Arjun Arora

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 various 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

Your Answer

Interviews

Parent Categories