Using the square bracket with conditional selection in the data frame, you can subset the data frame in r and extract the observations you would like to keep or exclude.
You can also subsetting the data frame in r using the subset() function in R.
In this tutorial, we will discuss how to subset rows of data from the data frame using the logical expression in R.
Subset Rows with Equal To condition
Using the equal to (==) operator, you can subset the data frame.
Let’s consider an example to create a data frame in r to subset rows with == operator.
# Create a data frame
student_info <- data.frame(
name = c("Tom","Kim","Sam","Julie","Emily","Chris"),
age = c(20,21,19,20,21,22),
gender = c('M','F','M','F','F','M'),
marks = c(72,77,65,80,85,87)
)
# Print the data frame
student_info
The output of the above data frame is:
name age gender marks
1 Tom 20 M 72
2 Kim 21 F 77
3 Sam 19 M 65
4 Julie 20 F 80
5 Emily 21 F 85
6 Chris 22 M 87
Using the above data frame, extract the data for male candidates only using == operator.
# Display data for only Male candiate
student_info[student_info$gender =="M",]
In the above R code, the student_info data frame uses $gender variable to compare to the value “M” using == operator.
It subset data frame and displays male candidates data only.
name age gender marks
1 Tom 20 M 72
3 Sam 19 M 65
6 Chris 22 M 87
Subset data frame using multiple conditions
You can specify multiple conditions to subset a data frame to extract the data.
Let’s consider the student_info data frame to extract data for female candidates where marks is greater than 80
# Display data for female candiate where marks is greater than 80
student_info[student_info$gender =="F" & student_info$marks > 80,]
In the above r code, we have specified multiple conditions like gender is equal to female and marks is greater than 80.
name age gender marks
5 Emily 21 F 85
Data frame conditional selection using Relation operator
Using the relation operator in the condition, you can subset a data frame.
Let’s consider the above student_info data frame to get students’ data where age is between the specified age.
# Display data for students having age between 20 and 21
student_info[student_info$age > 20 & student_info$age <= 21, ]
In the above R code to subset data frame by rows, it uses relation operator in condition to display students data having the age between 20 and 21.
name age gender marks
2 Kim 21 F 77
5 Emily 21 F 85
Subset of Data frame Rows with %in%
Using the %in% operator, you can extract and filter data where the column value is equal to specified values.
# Display data for students where marks is equal to 65 or 85
student_info[student_info$marks %in% c(65,85),]
The output of the above R code is:
name age gender marks
3 Sam 19 M 65
5 Emily 21 F 85
Conclusion
I hope the above article on subset data frame in r using the logical condition is helpful to you.
Using the relation operator with multiple conditions in the data frame, you can filter and extract data.