In this tutorial we will learn how to use dplyr’s between() function to find if one or more numerical values are in a range using multiple examples. We will first start with a single numeric value and find out if it is within a range. And then in the next example, we will use a numerical vector to find each of the values is between a range.
In the final examples we will apply between() function to a dataframe. Basically dplyr’s between() function is a nice little shortcut for finding
x >= left & x <= right
.
Let us load the packages needed and check the version of dplyr
library(tidyverse) library(palmerpenguins) pacakageVersion("dplyr") [1] '1.0.9'
The basic usage dplyr’s between() function is as follows
between(x, left, right),
where x is a numerical value or numerical vector and left and right define the range we are interested in.
dplyr between() Example with a numerical value
x <- runif(1, 5,10) x [1] 5.999246
between(x, left= 5, right=20) [1] TRUE
dplyr between() Example with a numerical vector
With dplyr’s between() function we can easily answer if values in a numerical vector are between a specific range.
For example if you want to find out if all values in a numerical vector is between 1 and 10, between() function can be very handy.
It will give us a boolean vector with TRUE for values that are within the specified range and FALSE for the values that are not in the range.
x <- round(rnorm(10, mean=5, sd= 100),1) x [1] 45.2 33.5 -120.3 196.0 -132.9 -172.7 -8.0 -8.8 -75.2 23.0
between(x, left = -50, right = 50) [1] TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
dplyr between() Example with a dataframe
penguins %>% head() # A tibble: 6 × 8 species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex <fct> <fct> <dbl> <dbl> <int> <int> <fct> 1 Adelie Torge… 39.1 18.7 181 3750 male 2 Adelie Torge… 39.5 17.4 186 3800 fema… 3 Adelie Torge… 40.3 18 195 3250 fema… 4 Adelie Torge… NA NA NA NA <NA> 5 Adelie Torge… 36.7 19.3 193 3450 fema… 6 Adelie Torge… 39.3 20.6 190 3650 male # … with 1 more variable: year <int>
In this example, we use dplyr’s between() function to add a new logical column that tells you if the body mass values are within a range of interest.
penguins %>% select(species, body_mass_g) %>% mutate(within_range =between(body_mass_g, 5000, 5500)) # A tibble: 344 × 3 species body_mass_g within_range <fct> <int> <lgl> 1 Adelie 3750 FALSE 2 Adelie 3800 FALSE 3 Adelie 3250 FALSE 4 Adelie NA NA 5 Adelie 3450 FALSE 6 Adelie 3650 FALSE 7 Adelie 3625 FALSE 8 Adelie 4675 FALSE 9 Adelie 3475 FALSE 10 Adelie 4250 FALSE # … with 334 more rows
dplyr between() Example for filtering a dataframe
Here we use dplyr’s between() function to filter the dataframe based on body mass values in a range of interest.
penguins %>% select(species, body_mass_g) %>% filter(between(body_mass_g, 5000, 5500)) # A tibble: 39 × 2 species body_mass_g <fct> <int> 1 Gentoo 5400 2 Gentoo 5200 3 Gentoo 5150 4 Gentoo 5350 5 Gentoo 5000 6 Gentoo 5050 7 Gentoo 5000 8 Gentoo 5100 9 Gentoo 5250 10 Gentoo 5050 # … with 29 more rows