Select Data Frame Rows where Column Values are in Range in R (2 Examples)
In this article you’ll learn how to extract certain data frame rows within a range of values in the R programming language.
Table of contents:
So now the part you have been waiting for – the examples…
Creation of Example Data
The following data will be used as basement for this R programming tutorial:
data <- data.frame(x1 = c(5, 9, 1, 4, 3, 9), # Creating example data x2 = letters[7:2], x3 = 33) data # Printing example data # x1 x2 x3 # 1 5 g 33 # 2 9 f 33 # 3 1 e 33 # 4 4 d 33 # 5 3 c 33 # 6 9 b 33
Have a look at the previous output of the RStudio console. It shows that our example data has six rows and three columns.
Example 1: Return Rows with Column Values in Certain Range Using Square Brackets
Example 1 explains how to subset rows from our data frame where the values of the variable x1 are ranging between 3 and 5. For this, we first have to identify the rows that are in this range:
identify_rows <- data$x1 >= 3 & data$x1 <= 5 # Identify rows in range identify_rows # Printing logical indicator # TRUE FALSE FALSE TRUE TRUE FALSE
The previous R code created a logical vector where TRUE indicates that the values of the column x1 are between 3 and 5 and FALSE indicates that x1 is outside of the range.
Now, we can use this vector to subset our data conditionally:
data_range1 <- data[identify_rows, ] # Subsetting data frame data_range1 # Printing new data frame # x1 x2 x3 # 1 5 g 33 # 4 4 d 33 # 5 3 c 33
Our data frame subset consists of three rows. All values of the variable x1 are within our previously defined range.
Example 2: Return Rows with Column Values in Certain Range Using subset() Function
Alternatively to the R syntax shown in Example 1, we can also use the subset function to extract data frame rows in a certain range. Have a look at the following R code:
data_range2 <- subset(data, identify_rows) # Applying subset function data_range2 # Printing new data frame # x1 x2 x3 # 1 5 g 33 # 4 4 d 33 # 5 3 c 33
The resulting data table is exactly the same as in Example 1.
Video, Further Resources & Summary
I have recently published a video on my YouTube channel, which explains the content of this article. You can find the video below:
In addition to the video, you might have a look at some of the related tutorials which I have published on my website. You can find a selection of articles about topics such as data inspection, vectors, and character strings here:
- Find Rows in First Data Frame that are not in Second
- Select Data Frame Column Using Character Vector
- Select Data Frame Rows based on Values in Vector
- Introduction to R Programming
In summary: In this tutorial, I explained how to return only rows in a specific value range in the R programming language. Don’t hesitate to let me know in the comments section, if you have any additional questions.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






