Skip to content

Instantly share code, notes, and snippets.

@xvrdm
Created December 2, 2020 09:52
Show Gist options
  • Save xvrdm/5f1bacaef8de15740f96ccca177d7f02 to your computer and use it in GitHub Desktop.
Save xvrdm/5f1bacaef8de15740f96ccca177d7f02 to your computer and use it in GitHub Desktop.
{dplyr}'s between cannot take multiple values vectors for left/right boundaries
> between(c(5,6), left = c(1,4), right = c(2,10))
[1] FALSE FALSE
> tibble(val = c(5,6), min_threshold = c(1,4), max_threshold = c(2,10)) %>%
mutate(is_in_range = between(val, min_threshold, max_threshold))
# A tibble: 2 x 4
val min_threshold max_threshold is_in_range
<dbl> <dbl> <dbl> <lgl>
1 5 1 2 FALSE
2 6 4 10 FALSE
> tibble(val = c(5,6), min_threshold = c(1,4), max_threshold = c(2,10)) %>%
rowwise() %>%
mutate(is_in_range = between(val, min_threshold, max_threshold))
# A tibble: 2 x 4
# Rowwise:
val min_threshold max_threshold is_in_range
<dbl> <dbl> <dbl> <lgl>
1 5 1 2 FALSE
2 6 4 10 TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment