Skip to contents

Check that x is a numeric vector of the correct length with numbers of the correct sign.

Usage

is_number(x, allow_zero = FALSE, allow_NA = FALSE, allow_NaN = FALSE)

all_numbers(x, allow_zero = FALSE, allow_NA = FALSE, allow_NaN = FALSE)

is_nonnegative(x, allow_zero = FALSE, allow_NA = FALSE, allow_NaN = FALSE)

all_nonnegative(x, allow_zero = FALSE, allow_NA = FALSE, allow_NaN = FALSE)

is_positive(x, allow_zero = FALSE, allow_NA = FALSE, allow_NaN = FALSE)

Arguments

x

object to test.

allow_zero

TRUE or FALSE: allow zero-length x of the correct type?

allow_NA

TRUE or FALSE: allow numeric NAs (i.e., NA_integer_ and NA_real_)?

allow_NaN

TRUE or FALSE: allow NaNs?

Value

TRUE or FALSE indicating if x is a numeric vector of the correct length with numbers of the correct sign that adheres to the limitations set by the other arguments.

Details

The correct length of x is one for is_...() and larger than zero for all_...(), unless allow_zero is TRUE: then numeric-type zero-length x is also allowed for both types of functions.

all_nonnegative() and is_nonnegative() return TRUE for 0, whereas is_positive() returns FALSE for 0.

all_...() and is_...() return TRUE for -Inf and Inf if it has the correct sign.

all_...() and is_...() return FALSE for NA_complex_, even if allow_NA is TRUE, because its mode is complex instead of numeric.

NaN has mode numeric, despite meaning 'not a number'.

Programming notes

is.numeric() tests the mode() of x, which is numeric for floating-point numbers such as 3.2 and integers such as 3L. In contrast, class(x) == "numeric" would test the class() of x which is numeric for floating-point numbers but integer for integers (see the Note on names in is.numeric()).

The functions duplicate code instead of calling is_number() or all_numbers(), to prevent performing checks twice.

See also

The vignettes about design choices and about type coercion.

Other collections of checks on type and length: all_characters(), all_names(), is_logical(), is_natural(), is_zerolength()

Examples

is_number(x = 1) # TRUE
#> [1] TRUE
is_number(x = 3.14) # TRUE
#> [1] TRUE
is_number(x = c(1, 2)) # FALSE: incorrect length
#> [1] FALSE
all_numbers(x = c(1, 2)) # TRUE
#> [1] TRUE
is_number(x = "a") # FALSE: incorrect type
#> [1] FALSE
is_number(x = numeric(0)) # FALSE: incorrect length
#> [1] FALSE
is_number(x = numeric(0), allow_zero = TRUE) # TRUE
#> [1] TRUE
is_number(x = NA_real_) # FALSE
#> [1] FALSE
is_number(x = NA_real_, allow_NA = TRUE) # TRUE
#> [1] TRUE
is_number(x = NA_character_, allow_NA = TRUE) # FALSE: incorrect type
#> [1] FALSE
is_number(x = NaN, allow_NA = TRUE) # FALSE, need allow_NaN = TRUE to allow NaN
#> [1] FALSE
is_number(x = NaN, allow_NaN = TRUE) # TRUE
#> [1] TRUE
is_number(x = Inf) # TRUE
#> [1] TRUE
is_nonnegative(x = 3) # TRUE
#> [1] TRUE
is_nonnegative(x = 0) # TRUE
#> [1] TRUE
all_nonnegative(x = c(3, 0)) # TRUE
#> [1] TRUE
all_nonnegative(x = numeric(0), allow_zero = TRUE) # TRUE
#> [1] TRUE
is_positive(x = 3) # TRUE
#> [1] TRUE
is_positive(x = 0) # FALSE
#> [1] FALSE