Open In App

Subset Data Frames Using Logical Conditions In R

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads