Open In App

How to Conditionally Remove Rows in R DataFrame?

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Method 1: Remove Row by Single Condition

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,

  1. df: determines the dataframe to be used.
  2. 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 data
sample_data <- data.frame( x = rnorm(10),
                          y=rnorm(10,20) )
# print data
print("Sample Data:")
sample_data
  
# filter data
new_data = sample_data[sample_data$x > 0, ]
  
# print data
print("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

Method 2: Remove Row by Multiple Condition

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 data
sample_data <- data.frame( x = rnorm(10),
                          y=rnorm(10,20) )
# print data
print("Sample Data:")
sample_data
  
# filter data
new_data = sample_data[sample_data$x > 0 & sample_data$y > 0.4, ]
  
# print data
print("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

Method 3 : Remove Rows by subset() function

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 data
sample_data <- data.frame( x = rnorm(10,20),
                          y=rnorm(10,50) )
# print data
print("Sample Data:")
sample_data
  
# filter data
new_data = subset(sample_data, sample_data$x > 19 & sample_data$y < 49 )
  
# print data
print("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


Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads