Select DataFrame Rows where Column Values are in Range in R - GeeksforGeeks

23 Sep, 2021
In this article, we will discuss how to select dataframe rows where column values are in a range in R programming language.
Data frame indexing can be used to extract rows or columns from the dataframe. The condition can be applied to the specific columns of the dataframe and combined using the logical operator. The logical values in terms of TRUE or FALSE are returned where the TRUE  values indicate the rows satisfying the condition. The selected rows are then extracted using the returned row indexes. 
Syntax:
dataframe[condition to define range]
Example: Select rows where column values are in the range
R
# create first dataframedata_frame1 < -data.frame(col1=c(rep('Grp1', 2),                                 rep('Grp2', 2),                                 rep('Grp3', 2)),                          col2=rep(1: 3, 2),                          col3=rep(1: 2, 3),                          col4=letters[1:6]                          )  print("Original DataFrame")print(data_frame1)  filtered_rows < - data_frame1$col2 >= 2 & data_frame1$col2 < 4data_frame_mod < - data_frame1[filtered_rows, ]  print("Extracted DataFrame")print(data_frame_mod)
Output:
[1] "Original DataFrame" 
col1 col2 col3 col4 
1 Grp1    1    1    a 
2 Grp1    2    2    b 
3 Grp2    3    1    c 
4 Grp2    1    2    d 
5 Grp3    2    1    e 
6 Grp3    3    2    f 
[1] "Extracted DataFrame" 
col1 col2 col3 col4 
2 Grp1    2    2    b 
3 Grp2    3    1    c 
5 Grp3    2    1    e 
6 Grp3    3    2    f
Conditions can also be applied over multiple columns of the dataframe and connected using a logical operator. 
Example: Selecting columns by applying conditions over multiple columns. 
R
# create first dataframedata_frame1<-data.frame(col1=c(rep('Grp1',2),                               rep('Grp2',2),                               rep('Grp3',2)),                         col2=rep(1:3,2),                        col3=rep(1:2,3),                        col4 = letters[1:6]                        )  print("Original DataFrame")print(data_frame1)  filtered_rows <- data_frame1$col4 == 'a' | data_frame1$col3==2data_frame_mod <- data_frame1[filtered_rows ,]  print("Extracted DataFrame")print(data_frame_mod)
Output:
[1] "Original DataFrame" 
col1 col2 col3 col4 
1 Grp1    1    1    a 
2 Grp1    2    2    b 
3 Grp2    3    1    c 
4 Grp2    1    2    d 
5 Grp3    2    1    e 
6 Grp3    3    2    f 
[1] "Extracted DataFrame" 
 col1 col2 col3 col4 
1 Grp1    1    1    a 
2 Grp1    2    2    b 
4 Grp2    1    2    d 
6 Grp3    3    2    f
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R