+1 vote
1 view
in R Programming by (6.5k 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 (25.3k points)
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 beginner:

+1 vote
by (31.4k points)

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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...