Skip to contents

This convenience function tests whether elements of a numeric vector x lie within a specified numeric interval range. It is similar to data.table::between(), except that it allows a more flexible specification of open and closed interval boundaries. An infix operator %inrange% is provided, which includes the lower and excludes the upper bound.

Usage

inrange(x, range, closed_interval)

x %inrange% range

Arguments

x

Numeric vector to test.

range

Numeric vector of length 2 specifying the interval: c(lower, upper). Must satisfy range[1] <= range[2].

closed_interval

Logical vector of length 2 indicating whether to include the lower and upper bounds, respectively. Parameter is controlled by the infix operators.

Value

A logical vector of the same length as x, indicating TRUE for elements within the specified interval.

Examples

x <- seq(0, 5, 0.5)
x %inrange% c(1, 2)
#>  [1] FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
identical(inrange(x, c(1, 2), closed_interval = c(TRUE, FALSE)), x %inrange% c(1, 2))
#> [1] TRUE
inrange(x, c(1, 2), closed_interval = c(TRUE, TRUE))
#>  [1] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE