Using the subset() function in R, we can select rows by conditions specified in the square bracket notation for the data frame. We can specify the logical condition with operators to select rows by the condition in R.
The Subsetting data frame returns a subset of the data frame which meets the condition.
subset syntax:
subset(x, ...)
where x = data frame object to be subsetted.
In this tutorial, we will discuss how to select rows by the condition in R, subset rows with filter in R.
Select Rows in R using subset
Using the subset() function in R, we can select the rows from a data frame in R based on condition.
Let’s practice with an example to select a row from a data frame in R.
Create a data frame using the data.frame() function.
# Create a data frame
emp_info <- data.frame(
id = c(1,2,3,4,5,6),
name = c("Henry","Gary","Sam","Julie","Kim","Chris"),
age = c(28,24,29,25,23,24),
salary = c(4500,4000,5500,3650,3400,3800)
)
# Print data frame
emp_info
In the above R code, it creates a data frame of 4 columns and 6 rows and prints the output as below.
id name age salary
1 1 Henry 28 4500
2 2 Gary 24 4000
3 3 Sam 29 5500
4 4 Julie 25 3650
5 5 Kim 23 3400
6 6 Chris 24 3800
Let’s select a row in R where the column matches the condition.
# Filter the rows based on the condition
subset(emp_info, emp_info$name == 'Gary')
In the above R code, we have used the subset() function to specify the data frame to be subsetted and select rows where name == Gary
The output of the above R code is:
id name age salary
2 2 Gary 24 4000
Another way to filter rows from a data frame is by specifying the name of the data frame and condition.
# Filter out rows from a data frame
subset(emp_info,name == 'Gary')
The output of the above R code select row from R based on a condition where employee name is equal to Gary
id name age salary
2 2 Gary 24 4000
Use subset to Select Rows in R based on Condition
Let’s consider an example to filter rows from a data frame based on the conditions using the subset function in R.
Use the above emp_info data frame for illustration purposes.
Let’s select rows from a data frame where salary is greater than 4000
subset(emp_info, emp_info$salary > 4000)
In the above R code, the subset function in R takes the data frame as input and filter the rows from a data frame where salary is greater than 4000.
The output of the above R code selects one more row from a data frame where salary is greater than 4000.
id name age salary
1 1 Henry 28 4500
3 3 Sam 29 5500
Filter Rows in Data Frame using dplyr
Using the filter function of the dplyr package, we can filter the rows from a data frame.
Let’s use the above data frame to select rows from a data frame using filter() from the dplyr package.
filter(emp_info,salary==5500)
In the above filter function, it takes a data frame and logical condition as an input to filter out the rows in a data frame.
The output of the above R code is:
id name age salary
1 3 Sam 29 5500
Conclusion
I hope the above article on how to select rows by the condition in R is helpful to you.
Using the subset() function and filter() function of the dplyr package, we can select one or more rows from a data frame based on the condition.