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. |
filter(.data, ...)
We’ll use the following argument values:
.data
: A tibble or a lazy data frame (from dtplyr
or dbplyr
)....
: additional argumentsIt will return an instance of the same datatype as .data
.
Can we use it for both grouped and ungrouped data?
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)
data.frame()
with three features and six observations to create DataFrame.filter()
method with filter condition x < 5
and z== TRUE
.%in%
operatorlibrary(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"))
data.frame()
with three features and five observations to create a DataFrame.filter()
method with a filter condition search only for Cancer and Taurus.