Trusted answers to developer questions

What is the filter() Method in R?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

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

[xor()]

Logical XOR operation

[is.na()]

Either value is NA or not

[between()]

Checks whether the numerical value(Column value) lies between the specified range or not.

[near()]

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 (from dtplyr or dbplyr).
  • ...: additional arguments

Return value

It will return an instance of the same datatype as .data.

Question

Can we use it for both grouped and ungrouped data?

Show Answer

Explanation

The code snippet below demonstrates how to use the filter() method in a program.

library(dplyr, warn.conflicts = FALSE)
# Creat a DataFrame
df= 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 filter
filter(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 condition x < 5 and z== TRUE.

Using %in%operator

library(dplyr, warn.conflicts = FALSE)
# Create a DataFrame
df=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.

RELATED TAGS

filter
r programming
Did you find this helpful?