Subset Data Frames Using Logical Conditions In R
Last Updated :
02 Apr, 2024
In this article, we will explore various methods of Subset data frames using logical conditions using the R Programming Language.
How to Subset data frames using logical conditions
R language offers various methods to subset data frames using logical conditions. By using these methods provided by R, it is possible to subset a data frame. Some of the methods to subset data frames using logical conditions are:
Subset data frame with ==
This method is used to perform subsetting on a data frame. In the below example, we created a data frame and performed subsetting by using logical conditions.
R
#creating data frame
df <- data.frame( a1 = c(3, 7, 1, 8, 5,8),
a2 = letters[3:8],
batch = c("b0","b1","b2","b1","b3","b1"))
print(df)
print("After subsetting the data frame")
df[df$batch == "b1", ]
Output:
a1 a2 batch
1 3 c b0
2 7 d b1
3 1 e b2
4 8 f b1
5 5 g b3
6 8 h b1
[1] "After subsetting the data frame"
a1 a2 batch
2 7 d b1
4 8 f b1
6 8 h b1
Subset data frame with %in%
This method is used to perform subsetting on a data frame. In this below example, we created a data frame and performed subsetting by using logical condition.
R
# creating data Frame
df <- data.frame(
name = c("a","b","c","d","e"),
id = c(15, 30, 45, 60, 75),
batch=c("a0","b1","b2","b1","c1")
)
print(df)
print("After subsetting the data frame ")
res=df[df$batch %in% c("b1", "c1"), ]
print(res)
Output:
name id batch
1 a 15 a0
2 b 30 b1
3 c 45 b2
4 d 60 b1
5 e 75 c1
[1] "After subsetting the data frame "
name id batch
2 b 30 b1
4 d 60 b1
5 e 75 c1
Subset data frame with !=
This method is used to perform subsetting on a data frame. In this below example, we created a data frame and performed subsetting by using logical condition.
R
#creating data frame
df <- data.frame( a1 = c(3, 7, 1, 8, 5,8),
a2 = letters[3:8],
batch = c("b0","b1","b2","b1","b3","b1"))
print(df)
print("After subsetting the data frame")
df[df$batch != "b1", ]
Output:
a1 a2 batch
1 3 c b0
2 7 d b1
3 1 e b2
4 8 f b1
5 5 g b3
6 8 h b1
[1] "After subsetting the data frame"
a1 a2 batch
1 3 c b0
3 1 e b2
5 5 g b3
Subsetting data frame by using Square Brackets [ ]:
This method is used to perform subsetting on a data frame. In this below example, we created a data frame and performed subsetting by using logical condition
R
# creating data Frame
df <- data.frame(
name = c("a","b","c","d","e"),
id = c(15, 30, 45, 60, 75),
batch=c("a0","b1","b2","a2","c1")
)
print(df)
print("After subsetting the data frame ")
subset_df <- df[df$id > 30, ]
print(subset_df)
Output:
name id batch
1 a 15 a0
2 b 30 b1
3 c 45 b2
4 d 60 a2
5 e 75 c1
[1] "After subsetting the data frame "
name id batch
3 c 45 b2
4 d 60 a2
5 e 75 c1
Subsetting data frame by using subset() Function
This method is used to perform subsetting on a data frame. In this below example, we created a data frame and performed subsetting by using logical condition.
R
# creating data Frame
df <- data.frame(
name = c("a","b","c","d","e"),
id = c(15, 30, 45, 60, 75),
batch=c("a0","b1","b2","a2","c1")
)
print(df)
print("After subsetting the data frame ")
subset_df <- subset(df, id > 45 )
print(subset_df)
Output:
name id batch
1 a 15 a0
2 b 30 b1
3 c 45 b2
4 d 60 a2
5 e 75 c1
[1] "After subsetting the data frame "
name id batch
4 d 60 a2
5 e 75 c1
Conclusion
In conclusion, we learned about how to perform subsetting by using logical conditions. R language offers versatile tools while performing subsetting on various data sets.
Similar Reads
Filter data by multiple conditions in R using Dplyr In this article, we will learn how can we filter dataframe by multiple conditions in R programming language using dplyr package. The filter() function is used to produce a subset of the data frame, retaining all rows that satisfy the specified conditions. The filter() method in R programming languag
3 min read
Analyzing Data in Subsets Using R In this article, we will explore various methods to analyze data in subsets using R Programming Language. How to analyze data in the subsetsAnalyzing data encompasses employing diverse methodologies to acquire insights, recognize patterns, and draw significant conclusions from datasets. This encompa
4 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
Filter Rows Based on Conditions in a DataFrame in R 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
3 min read
How to plot a subset of a dataframe in R ? In this article, we will learn multiple approaches to plotting a subset of a Dataframe in R Programming Language. Here we will be using, R language's inbuilt "USArrests" dataset. Method 1: Using subset() function In this method, first a subset of the data is created base don some condition, and then
2 min read