library(tidyverse)
library(palmerpenguins)
data(penguins)dplyr::between()
Function of the Week
Detect where values fall in a specified range
1 between( )
In this document, I will introduce the between( ) function and show what it can be used for.
1.1 What is it for?
The between( ) function from the dplyr package makes it easier to find values within a specified range (inclusive of the lower and upper bounds).
1.1.1 Example 1
#1 getting info on penguin body mass
summary(penguins$body_mass_g) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
2700 3550 4050 4202 4750 6300 2
#2 Getting values that are between the 1st and 3rd quartiles using >= , <=
penguins_filtered <- penguins %>%
filter(body_mass_g>= 3550 & body_mass_g <= 4750)
head(penguins_filtered)# A tibble: 6 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 39.3 20.6 190 3650
4 Adelie Torgersen 38.9 17.8 181 3625
5 Adelie Torgersen 39.2 19.6 195 4675
6 Adelie Torgersen 42 20.2 190 4250
# ℹ 2 more variables: sex <fct>, year <int>
# Getting values that are between the 1st and 3rd quartiles using between()
penguins_filtered_btwn<- penguins %>%
filter(between(body_mass_g, 3550, 4750))
head(penguins_filtered_btwn)# A tibble: 6 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 39.3 20.6 190 3650
4 Adelie Torgersen 38.9 17.8 181 3625
5 Adelie Torgersen 39.2 19.6 195 4675
6 Adelie Torgersen 42 20.2 190 4250
# ℹ 2 more variables: sex <fct>, year <int>
1.1.2 Example 2
#1 getting info on penguin bill length
summary(penguins$bill_length_mm) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
32.10 39.23 44.45 43.92 48.50 59.60 2
#2 creating dataset of penguins with shirt beaks
Penguins_short_beaks <- penguins%>%
filter(between(bill_length_mm, 32.10, 39.23))
head(Penguins_short_beaks)# A tibble: 6 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 36.7 19.3 193 3450
3 Adelie Torgersen 38.9 17.8 181 3625
4 Adelie Torgersen 39.2 19.6 195 4675
5 Adelie Torgersen 34.1 18.1 193 3475
6 Adelie Torgersen 37.8 17.1 186 3300
# ℹ 2 more variables: sex <fct>, year <int>
1.2 Is it helpful?
Discuss whether you think this function is useful for you and your work. Is it the best thing since sliced bread, or is it not really relevant to your work?
Yes, it works well with other functions such as mutate or filter, and also makes the readability of your code better. Readability of your code is important because it makes it easier for you to read and find errors, and makes it easier for others to work with your code.