between: Detect where values fall in a specified range

View source: R/funs.R

betweenR Documentation

Detect where values fall in a specified range

Description

This is a shortcut for x >= left & x <= right, implemented for local vectors and translated to the appropriate SQL for remote tables.

Usage

between(x, left, right, ..., ptype = NULL)

Arguments

x

A vector

left, right

Boundary values. Both left and right are recycled to the size of x.

...

These dots are for future extensions and must be empty.

ptype

An optional prototype giving the desired output type. The default is to compute the common type of x, left, and right using vctrs::vec_cast_common().

Details

x, left, and right are all cast to their common type before the comparison is made. Use the ptype argument to specify the type manually.

Value

A logical vector the same size as x with a type determined by ptype.

See Also

join_by() if you are looking for documentation for the between() overlap join helper.

Examples

between(1:12, 7, 9)

x <- rnorm(1e2)
x[between(x, -1, 1)]

# On a tibble using `filter()`
filter(starwars, between(height, 100, 150))

# Using the `ptype` argument with ordered factors, where otherwise everything
# is cast to the common type of character before the comparison
x <- ordered(
  c("low", "medium", "high", "medium"),
  levels = c("low", "medium", "high")
)
between(x, "medium", "high")
between(x, "medium", "high", ptype = x)

hadley/dplyr documentation built on Nov. 6, 2024, 4:48 p.m.