Filter Rows Based on Conditions in a DataFrame in R
Last Updated :
27 Mar, 2024
In this article, we will explore various methods to filter rows based on Conditions in a data frame by using the R Programming Language.
How to filter rows based on Conditions in a data frame
R language offers various methods to filter rows based on Conditions in a data frame. By using these methods provided by R, it is possible to filter rows. Some of the methods to filter rows based on conditions are:
Filter rows based on a single condition
This method is used to filter rows based on a single condition. In the below example, we created a data frame and filtered rows based on a single condition.
R
df <- data.frame( name=c("a","b","c","d","e","f"),
id=c(100,250,300,450,500,600),
age=c(10,20,30,35,40,50)
)
print(df)
res <- df[df$id > 400, ]
print("The resultant data frame is")
print(res)
Output:
name id age
1 a 100 10
2 b 250 20
3 c 300 30
4 d 450 35
5 e 500 40
6 f 600 50
[1] "The resultant data frame is"
name id age
4 d 450 35
5 e 500 40
6 f 600 50
In the below example, we created a data frame and filtered rows based on a single condition.
R
df <- data.frame( name=c("a","b","c","d","e","f"),
id=c(100,250,300,450,500,600),
age=c(10,20,30,35,40,50)
)
print(df)
res <- df[df$age > 35, ]
print("The resultant data frame is")
print(res)
Output:
name id age
1 a 100 10
2 b 250 20
3 c 300 30
4 d 450 35
5 e 500 40
6 f 600 50
[1] "The resultant data frame is"
name id age
5 e 500 40
6 f 600 50
Filter rows based on Multiple conditions
These method is used to filter the rows based on multiple conditions. In the below example, we created a data frame and filtered rows based on multiple conditions.
R
df <- data.frame( name=c("a","b","c","d","e","f"),
id=c(100,250,300,450,500,600),
age=c(10,20,30,35,40,50)
)
print(df)
res <- df[df$age > 30 & df$id == 450, ]
print("The resultant data frame is")
print(res)
Output:
name id age
1 a 100 10
2 b 250 20
3 c 300 30
4 d 450 35
5 e 500 40
6 f 600 50
[1] "The resultant data frame is"
name id age
4 d 450 35
In the below example, we created a data frame and filtered rows based on multiple conditions.
R
df <- data.frame( name=c("a","b","c","d","e","f"),
id=c(100,250,300,450,500,600),
age=c(10,20,30,35,40,50)
)
print(df)
res <- df[df$name %in% c("b", "e"), ]
print("The resultant data frame is")
print(res)
Output:
name id age
1 a 100 10
2 b 250 20
3 c 300 30
4 d 450 35
5 e 500 40
6 f 600 50
[1] "The resultant data frame is"
name id age
2 b 250 20
5 e 500 40
Conclusion
In conclusion, we learned about how to filter rows based on conditions in a data frame. R programming language offers versatile tools while handling to filter rows based on conditions in a data frame.
Similar Reads
Delete rows in PySpark dataframe based on multiple conditions In this article, we are going to see how to delete rows in PySpark dataframe based on multiple conditions. Method 1: Using Logical expression Here we are going to use the logical expression to filter the row. Filter() function is used to filter the rows from RDD/DataFrame based on the given conditio
2 min read
Pyspark - Filter dataframe based on multiple conditions In this article, we are going to see how to Filter dataframe based on multiple conditions. Let's Create a Dataframe for demonstration: Python3 # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving an
3 min read
Filtering rows based on column values in PySpark dataframe In this article, we are going to filter the rows based on column values in PySpark dataframe. Creating Dataframe for demonstration:Python3 # importing module import spark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving an app n
2 min read
Split Spark DataFrame based on condition in Python In this article, we are going to learn how to split data frames based on conditions using Pyspark in Python. Spark data frames are a powerful tool for working with large datasets in Apache Spark. They allow to manipulate and analyze data in a structured way, using SQL-like operations. Sometimes, we
5 min read
Selecting rows in pandas DataFrame based on conditions Letâs see how to Select rows based on some conditions in Pandas DataFrame. Selecting rows based on particular column value using '>', '=', '=', '<=', '!=' operator. Code #1 : Selecting all the rows from the given dataframe in which 'Percentage' is greater than 80 using basic method. Python# im
6 min read