Learn R Programming

vetr (version 0.2.18)

all_bw: Verify Values in Vector are Between Two Others

Description

Similar to isTRUE(all(x >= lo & x <= hi)) with default settings, except that it is substantially faster and returns a string describing the first encountered violation rather than FALSE on failure.

Usage

all_bw(x, lo = -Inf, hi = Inf, na.rm = FALSE, bounds = "[]")

Value

TRUE if all values in x conform to the specified bounds, a string describing the first position that fails otherwise

Arguments

x

vector logical (treated as integer), integer, numeric, or character. Factors are treated as their underlying integer vectors.

lo

scalar vector of type coercible to the type of x, cannot be NA, use -Inf to indicate unbounded (default).

hi

scalar vector of type coercible to the type of x, cannot be NA, use Inf to indicate unbounded (default), must be greater than or equal to lo.

na.rm

TRUE, or FALSE (default), whether NAs are considered to be in bounds. Unlike with all(), for all_bw na.rm=FALSE returns an error string if there are NAs instead of NA. Arguably NA, but not NaN, should be considered to be in [-Inf,Inf], but since NA < Inf is NA we treat them as always being out of bounds.

bounds

character(1L) for values between lo and hi:

  • “[]” include lo and hi

  • “()” exclude lo and hi

  • “(]” exclude lo, include hi

  • “[)” include lo, exclude hi

Details

You can modify the comparison to be strictly greater/less than via the bounds parameter, and the treatment of NAs with na.rm. Note that NAs are considered to be out of bounds by default. While technically incorrect since we cannot know whether an NA value is in or out of bounds, this assumption is both conservative and convenient. Zero length x will always succeed.

If x and lo/hi are different types, lo/hi will be coerced to the type of x. When lo/hi are numeric and x is integer, if lo/hi values are outside of the integer range then that side will be treated as if you had used -Inf/Inf. -Inf and Inf mean lo and hi will be unbounded for all data types.

Examples

Run this code
all_bw(runif(100), 0, 1)
all_bw(runif(100) * 2, 0, 1)
all_bw(NA, 0, 1)              # This is does not return NA
all_bw(NA, 0, 1, na.rm=TRUE)

vec <- c(runif(100, 0, 1e12), Inf, 0)
all_bw(vec, 0)      # All +ve numbers
all_bw(vec, hi=0)   # All -ve numbers
all_bw(vec, 0, bounds="(]") # All strictly +ve nums
all_bw(vec, 0, bounds="[)") # All finite +ve nums

Run the code above in your browser using DataLab