Skip to contents

Check if an object is NA. Always return TRUE of FALSE, a logical vector of length one.

Usage

isNA(x)

Arguments

x

any R object.

Details

isNA returns TRUE if the argument is a single NA, i.e. it is atomic, has length one, and represents an NA value. In any other case isNA returns FALSE.

isNA is suitable for use in conditional constructs since it always returns a single value which is never NA.

Note that identical() distinguishes different types of NA, i.e. identical(x, NA) is TRUE only if x is NA (logical).

Note

The requirement that x is atomic means that isNA(list(NA)) gives FALSE.

For comparison, is.na(list(NA)) gives TRUE. The same holds for classed lists, such as is.na(structure(list(NA), class = "myclass")).

Value

TRUE or FALSE

Author

Georgi N. Boshnakov

See also

Examples

v <- c(1, NA, 3)
isNA(v[2]) # TRUE
#> [1] TRUE

## a vector of two or more Na's is not isNA
isNA(rep(NA,3)) # FALSE
#> [1] FALSE

## a list containing NA is not isNA
isNA(list(NA)) # FALSE
#> [1] FALSE
## ... but
is.na(list(NA))  # TRUE
#> [1] TRUE

## identical() distinguishes different types of NA:
class(v) # "numeric", not "integer"
#> [1] "numeric"

identical(v[2], NA)          # FALSE, NA on its own is "logical"
#> [1] FALSE
identical(v[2], NA_integer_) # FALSE
#> [1] FALSE
identical(v[2], NA_real_)    # TRUE
#> [1] TRUE


vi <- c(1L, NA_integer_, 3L)
isNA(vi[2]) # TRUE
#> [1] TRUE

class(vi) # "integer"
#> [1] "integer"
identical(vi[2], NA_integer_) # TRUE
#> [1] TRUE
identical(vi[2], NA_real_)    # FALSE
#> [1] FALSE

## is.na(NULL) would give a warning
isNA(NULL) # FALSE
#> [1] FALSE

## a length zero object is not NA, so isNA() returns FALSE:
isNA(logical(0)) # FALSE
#> [1] FALSE

## is.na() has a different remit and returns a 0-length vector:
is.na(logical(0))  # logical(0)
#> logical(0)