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
.
vec_between(vec, left, right, subset = TRUE)
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. |
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.
# 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)#> [1] TRUE# 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)#> [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE #> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE 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)#> [1] TRUE