dplyr between(): find if numerical values are within a range.

dplyr between()
Published

August 17, 2022

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 % head()

# A tibble: 6 × 8
  species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex  
                                             
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  
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 

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
                    
 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
            
 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