between Function of dplyr R Package (2 Examples)
In this tutorial, I’ll illustrate how to test whether a numeric value falls into a specified range with the between function of the dplyr package in R.
Table of contents:
- Example 1: Basic Application of between Function
- Example 2: Value Outside of Range
- Video, Further Resources & Summary
If you want to learn more about these contents, keep reading…
Example 1: Basic Application of between Function
If we want to apply the between() function, we need to install and load the dplyr package of the tidyverse first:
install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr package |
install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr package
Then, we also need to specify a value, an upper bound of our range, and a lower bound of our range:
x1 <- 5 # Define value left1 <- 3 # Define lower range right1 <- 8 # Define upper range |
x1 <- 5 # Define value left1 <- 3 # Define lower range right1 <- 8 # Define upper range
Now, we can apply the between function to test whether our values falls within our range:
between(x1, left1, right1) # Apply between function # TRUE |
between(x1, left1, right1) # Apply between function # TRUE
The RStudio console returns the logical value TRUE, indicating that our value is larger than the lower bound and smaller than the upper bound.
Example 2: Value Outside of Range
Let’s illustrate what happens when we check a value outside of our range. First, we need to specify some new values:
x2 <- 10 # Define value left2 <- 2 # Define lower range right2 <- 7 # Define upper range |
x2 <- 10 # Define value left2 <- 2 # Define lower range right2 <- 7 # Define upper range
Now, we can apply the between command as we already did in Example 1:
between(x2, left2, right2) # Apply between function # FALSE |
between(x2, left2, right2) # Apply between function # FALSE
This time the between function returns FALSE to the RStudio console, indicating that our value does not fall into our range.
Video, Further Resources & Summary
Do you need more explanations on the dplyr package? Then you could watch the following video which I have published on my YouTube channel. In the video, I illustrate the further examples for the dplyr package in the R programming language:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition to the video, you might have a look at the other articles on my website. Please find a selection of related articles here:
To summarize: This tutorial explained how to check whether numeric values fall into a specified range with the between function of the dplyr package in R. In case you have additional questions, let me know in the comments.
Statistics Globe Newsletter