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

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

The evaluation of `condition`

resulted in an `NA`

. The `if`

conditional must have either a `TRUE`

or `FALSE`

result.

n

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

n

This can happen accidentally as the results of calculations:

n

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

n

n

To test whether an object is missing use `is.na(x)`

rather than `x == NA`

.

n

n

See also the related errors:

n

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

n

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

n

`if (NULL) {}n## Error in if (NULL) { : argument is of length zeronnif (not logical) {}n## Error: argument is not interpretable as logicalnnif (c(TRUE, FALSE)) {}n## Warning message:n## the condition has length > 1 and only the first element will be usedn`

‘

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

n

`if (x == NULL || x == ) {n`

n

changed it to

n

`if (is.null(x) || x == ) {n`

‘

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

this works with `NA`

not for `NA`

n

`comments = c(no,yes,NA)n for (l in 1:length(comments)) {n #if (!is.na(comments[l])) print(comments[l])n if (comments[l] != NA) print(comments[l])n }n`

‘