vec_between: Subset a vector for values within a specified range

Description Usage Arguments Value Examples

View source: R/func_vec.R

Description

This function is a modified version of dplyr::between(). Unlike dplyr's version, vec_between returns the subset of the supplied vector within the specified boundaries. To mimic the behavior of dplyr::between(), you can set subset = FALSE.

Usage

1
vec_between(vec, left, right, subset = TRUE)

Arguments

vec

A numeric vector

left

The leftmost (lower) boundary value

right

The rightmost (upper) boundary value

subset

Logical: TRUE and only the values within the specified boundaries will be returned from the supplied vector. FALSE and only logical indicators will be.

Value

If subset = TRUE (default), this function returns the supplied vector subsetted within the specified boundaries. If subset = FALSE, this function returns a logical vector indicating whether a specific entry in the supplied vector is within the specified range.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Create a vector from 0 to 20
x <- seq(1,20)
# Save x_lower as the lower half of the vector
x_lower <- vec_between(vec = x, left = min(x), right = median(x))

# Save x_upper as the upper half of the vector
x_upper <- vec_between(vec = x, left = median(x), right = max(x))

# Combine the vectors and check its equal to the original
# Using all.equal because `vec_between()` coerces to numeric class
all.equal(c(x_lower, x_upper), x)

# Get the vector for all values between 5 and 10
x5_10 <- vec_between(vec = x, left = 5, right = 10)

# To return logical indices, like `dplyr::between()`,
# you can set `subset = FALSE`:
vec_between(vec = x, left = 5, right = 10, subset = FALSE)

# To return the vector, like with `dplyr::between()`, you would subset
# it as follows:

x_indexed <- x[vec_between(vec = x, left = 5, right = 10, subset = FALSE)]

# Check that the values are equal
all.equal(x5_10, x_indexed)

jdtrat/jdtools documentation built on Dec. 20, 2021, 10:05 p.m.