Type checks - NA values
is_na2.RdChecks if an object is NA, NA_integer_, NA_real_, NA_complex_, or
NA_character_. Differently from is.na() and
rlang::is_na()/rlang::are_na(), it allows to control the behavior for
NaN objects.
are_na2()is vectorized, returning a vector of same length asx, and errors for non-atomic objects.is_na2()returnsFALSEfor non-atomic objects, or if not all elements passare_na2(), andTRUEotherwise.
Arguments
- x
[
any] An object to test.- nan
[
TRUE|FALSE] What to return forNaNvalues.- types
[
character()|NULL] A character vector of allowedNAtypes (seeNA). Set toNULLto allow all types.
Value
[
logical(length(x))] Forare_*: the vectorized or result of the test.[
TRUE|FALSE] Foris_*: the scalar result of the test.
Examples
x <- c(1, Inf, -Inf, NaN, NA)
# are_na() is vectorized:
are_na2(x)
#> [1] FALSE FALSE FALSE FALSE TRUE
#> c(FALSE, FALSE, FALSE, FALSE, TRUE)
are_na2(x, nan = TRUE)
#> [1] FALSE FALSE FALSE TRUE TRUE
#> c(FALSE, FALSE, FALSE, TRUE, TRUE)
are_na2(x, nan = NA)
#> [1] FALSE FALSE FALSE NA TRUE
#> c(FALSE, FALSE, FALSE, NA, TRUE)
# Errors for non-atomic objects:
try(are_na2(list(NA, NA))) #> Error
#> Error in is.nan(x) : default method not implemented for type 'list'
# is_na() test for a scalar NA value:
is_na2(c(NA, NA)) #> FALSE
#> [1] FALSE
is_na2(NA) #> TRUE
#> [1] TRUE
is_na2(NaN, nan = TRUE) #> TRUE
#> [1] TRUE
# Use all(are_na2(x)) to test if all elements are NA:
all(are_na2(c(NA, NA))) #> TRUE
#> [1] TRUE
# Accept only NAs of specific types:
is_na2(NA_integer_, types = c("logical", "character")) #> FALSE
#> [1] FALSE
# To test for a single NA value, use the n argument:
is_na2(NA, n = 1) #> TRUE
#> [1] TRUE