How to Conditionally Remove Rows in R DataFrame? - GeeksforGeeks

19 Dec, 2021
In this article, we will discuss how to conditionally remove rows from a dataframe in the R Programming Language. We need to remove some rows of data from the dataframe conditionally to prepare the data. For that, we use logical conditions on the basis of which data that doesn’t follow the condition is removed. 
To remove rows of data from a dataframe based on a single conditional statement we use square brackets [ ] with the dataframe and put the conditional statement inside it. This slices the dataframe and removes all the rows that do not satisfy the given condition.
Syntax:
df[ conditional-statement ]
where,
df: determines the dataframe to be used.conditional-statement: determines the condition for filtering data.
df: determines the dataframe to be used.
conditional-statement: determines the condition for filtering data.
Example:
In this example. all data points where x variable is less than zero are removed.
R
# create sample datasample_data <- data.frame( x = rnorm(10),                          y=rnorm(10,20) )# print dataprint("Sample Data:")sample_data  # filter datanew_data = sample_data[sample_data$x > 0, ]  # print dataprint("Filtered Data:")new_data
Output:
Sample Data:
           x        y
1   1.0356175 19.36691
2  -0.2071733 21.38060
3  -1.3449463 19.56191
4  -0.5313073 19.49135
5   1.7880192 19.52463
6  -0.7151556 19.93802
7   1.5074344 20.82541
8  -1.0754972 20.59427
9  -0.2483219 19.21103
10 -0.8892829 18.93114
Filtered Data:
        x        y
1 1.035617 19.36691
5 1.788019 19.52463
7 1.507434 20.82541
10  1.0460800 20.05319
To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.
Syntax:
df[ conditional-statement & / | conditional-statement ]
where,
df: determines the dataframe to be used.
conditional-statement: determines the condition for filtering data.
Example:
In this example. all data points where the x variable is less than zero and the y variable is less than 19 are removed.
R
# create sample datasample_data <- data.frame( x = rnorm(10),                          y=rnorm(10,20) )# print dataprint("Sample Data:")sample_data  # filter datanew_data = sample_data[sample_data$x > 0 & sample_data$y > 0.4, ]  # print dataprint("Filtered Data:")new_data
Output:
Sample Data:
              x        y
1  -1.091923406 21.14056
2   0.870826346 20.83627
3   0.285727039 20.89009
4  -0.224661613 20.04137
5   0.653407459 19.01530
6   0.001760769 18.36436
7  -0.572623161 19.72691
8  -0.092852143 19.58567
9  -0.423781311 19.99482
10 -1.332091619 19.36539
Filtered Data:
            x        y
2 0.870826346 20.83627
3 0.285727039 20.89009
5 0.653407459 19.01530
6 0.001760769 18.36436
The subset() function creates a subset of a given dataframe based on certain conditions. This helps us to remove or select the rows of data with single or multiple conditional statements. The subset() function is an inbuilt function of the R Language and does not need any third-party package to be imported.
Syntax:
subset( df, Conditional-statement )
where,
df: determines the dataframe to be used.
conditional-statement: determines the condition for filtering data.
Example:
In this example. all data points where the x variable is less than 19 and the y variable is greater than 50 are removed using the subset function.
R
# create sample datasample_data <- data.frame( x = rnorm(10,20),                          y=rnorm(10,50) )# print dataprint("Sample Data:")sample_data  # filter datanew_data = subset(sample_data, sample_data$x > 19 & sample_data$y < 49 )  # print dataprint("Filtered Data:")new_data
Output:
Sample Data:
          x        y
1  20.38324 51.02714
2  20.36595 50.64125
3  20.44204 52.28653
4  20.34413 50.08981
5  20.51478 49.53950
6  20.35667 48.88035
7  19.89415 49.78139
8  21.61003 49.43653
9  20.66579 49.14877
10 20.70246 50.06486
Filtered Data:
         x        y
6 20.35667 48.88035
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 Split Column Into Multiple Columns in R DataFrame?
How to Change Axis Scales in R Plots?
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 multiple conditions?
How to filter R DataFrame by values in a column?
Convert Matrix to Dataframe in R