What is the filter() Method in R?
Overview
The filter() method in R is used to subset a data frame based on a provided condition. If a row satisfies the condition, it must produce TRUE. Otherwise, non-satisfying rows will return NA values. Hence, the row will be dropped.
Here listed, in the following table, are some functions and operators, used for filtering data that can be used as a breaking condition:
Condition | Description |
| Checks equality of the column values |
| Checks for greater column values |
| Checks for greater than or equal |
| Logical AND operation |
| Logical OR operation |
| Logical NOR operation |
| Logical XOR operation |
| Either value is NA or not |
| Checks whether the numerical value(Column value) lies between the specified range or not. |
| Compares numerical vector to nearest values. |
Syntax
filter(.data, ...)
Parameters
We’ll use the following argument values:
.data: A tibble or a lazy data frame (fromdtplyrordbplyr)....: additional arguments
Return value
It will return an instance of the same datatype as .data.
Can we use it for both grouped and ungrouped data?
Explanation
The code snippet below demonstrates how to use the filter() method in a program.
library(dplyr, warn.conflicts = FALSE)# Creat a DataFramedf= data.frame(x=c(2,3,4,5,7,4),y=c(1.1,2.1,4.5,45.1,3.2,66),z=c(TRUE,FALSE,TRUE,TRUE,FALSE,FALSE))# condition to filterfilter(df, x < 5 & z==TRUE)
- Line 3: We use
data.frame()with three features and six observations to create DataFrame. - Line 7: We invoke the
filter()method with filter conditionx < 5andz== TRUE.
Using %in%operator
library(dplyr, warn.conflicts = FALSE)# Create a DataFramedf=data.frame(x= c(1,2,3,4,5),y= c(2.1,4.5,8.2,10.4,50),z= c("Aries","Taurus", "Cancer", "Leo","Libra"))# invoking filter()df %>% filter(z %in% c("Cancer", "Taurus"))
- Line 3: We use
data.frame()with three features and five observations to create a DataFrame.
- Line 7: We invoking the
filter()method with a filter condition search only for Cancer and Taurus.