How to use filter() directly In R Language
For this simply the conditions to check upon are passed to the filter function, this function automatically checks the dataframe and retrieves the rows which satisfy the conditions.
Syntax: filter(df , condition)
Parameter :
df: The data frame object
condition: filtering based upon this condition
Example : R program to filter rows using filter() function
R
library (dplyr) # sample data df= data.frame (x= c (12,31,4,66,78), y= c (22.1,44.5,6.1,43.1,99), z= c ( TRUE , TRUE , FALSE , TRUE , TRUE )) # condition filter (df, x<50 & z== TRUE ) |
Output:
x y z 1 12 22.1 TRUE 2 31 44.5 TRUE
Filter data by multiple conditions in R using Dplyr
In this article, we will learn how can we filter dataframe by multiple conditions in R programming language using dplyr package.
The filter() function is used to produce a subset of the data frame, retaining all rows that satisfy the specified conditions. The filter() method in R programming language can be applied to both grouped and ungrouped data. The expressions include comparison operators (==, >, >= ) , logical operators (&, |, !, xor()) , range operators (between(), near()) as well as NA value check against the column values. The subset data frame has to be retained in a separate variable.