filter

0th

Percentile

Return rows with matching conditions

Use filter() find rows/cases where conditions are true. Unlike base subsetting with [, rows where the condition evaluates to NA are dropped.

Usage
filter(.data, ...)
Arguments
.data

A tbl. All main verbs are S3 generics and provide methods for tbl_df(), dtplyr::tbl_dt() and dbplyr::tbl_dbi().

...

Logical predicates defined in terms of the variables in .data. Multiple conditions are combined with &. Only rows where the condition evaluates to TRUE are kept.

These arguments are automatically quoted and evaluated in the context of the data frame. They support unquoting and splicing. See vignette("programming") for an introduction to these concepts.

Details

Note that dplyr is not yet smart enough to optimise filtering optimisation on grouped datasets that don't need grouped calculations. For this reason, filtering is often considerably faster on ungroup()ed data.

Value

An object of the same class as .data.

Useful filter functions

Tidy data

When applied to a data frame, row names are silently dropped. To preserve, convert to an explicit variable with tibble::rownames_to_column().

Scoped filtering

The three scoped variants (filter_all(), filter_if() and filter_at()) make it easy to apply a filtering condition to a selection of variables.

See Also

filter_all(), filter_if() and filter_at().

Other single table verbs: arrange, mutate, select, slice, summarise

Aliases
  • filter
Examples
# NOT RUN {
filter(starwars, species == "Human")
filter(starwars, mass > 1000)

# Multiple criteria
filter(starwars, hair_color == "none" & eye_color == "black")
filter(starwars, hair_color == "none" | eye_color == "black")

# Multiple arguments are equivalent to and
filter(starwars, hair_color == "none", eye_color == "black")
# }
Documentation reproduced from package dplyr, version 0.7.7, License: MIT + file LICENSE

Community examples

lpy199454@gmail.com at Jul 3, 2018 dplyr v0.7.6

# Remove align level comics <- comics %>% filter(align != "Reformed Criminals") %>% droplevels()

smitaudgata@gmail.com at May 14, 2018 dplyr v0.7.3

temp<-filter(starwars, species == "Human") temp

darrel_22 at Jan 16, 2018 dplyr v0.7.3

## Filter based on list of criteria With a long list of criteria on which you want to filter, using "&" can become cumbersome. To filter based on such a list for a given variable you can use the %in% operator: ```r list_of_values <- c("SI1", "SI2") SI_data <- filter(diamonds, clarity %in% list_of_values) ```