anyOutside: Are any values outside the interval specified?

View source: R/anyOutside.R

anyOutsideR Documentation

Are any values outside the interval specified?

Description

Are any values outside the interval specified?

Usage

anyOutside(x, a, b, nas_absent = NA, na_is_outside = NA)

Arguments

x

A numeric vector.

a, b

Single numeric values designating the interval.

nas_absent

Are NAs known to be absent from x? If nas_absent = NA, the default, x will be searched for NAs; if nas_absent = TRUE, x will not be checked; if nas_absent = FALSE, the answer is NA_integer_ if na.rm = FALSE otherwise only non-NA values outside [a, b].

If nas_absent = TRUE but x has missing values then the result is unreliable.

na_is_outside

(logical, default: NA) How should NAs in x be treated?

If NA

the default, then the first value in x that is either outside [a, b] or NA is detected: if it is NA, then NA_integer_ is returned; otherwise the position of that value is returned.

#'

If FALSE

then NA values are effectively skipped; the position of the first known value outside [a, b] is returned.

If TRUE

the position of the first value that is either outside [a, b] or NA is returned.

Value

0L if no values in x are outside [a, b]. Otherwise, the position of the first value of x outside [a, b].

Examples

anyOutside(1:10, 1L, 10L)
anyOutside(1:10, 1L, 7L)

# na_is_outside = NA
anyOutside(c(1:10, NA), 1L, 7L)     # Already outside before the NA
anyOutside(c(NA, 1:10, NA), 1L, 7L) # NA since it occurred first

anyOutside(c(1:7, NA), 1L, 7L, na_is_outside = FALSE)
anyOutside(c(1:7, NA), 1L, 7L, na_is_outside = TRUE)

##
# N <- 500e6
N <- 500e3
x <- rep_len(hutils::samp(-5:6, size = 23), N)
bench_system_time(anyOutside(x, -5L, 6L))
#    process      real
#  453.125ms 459.758ms


hutilscpp documentation built on Oct. 11, 2023, 9:06 a.m.