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

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 conditon evalutes 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.

Value

An object of the same class as .data.

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.

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

Examples

filter(starwars, species == "Human")
#> # A tibble: 35 × 13 #> name height mass hair_color skin_color eye_color #> <chr> <int> <dbl> <chr> <chr> <chr> #> 1 Luke Skywalker 172 77 blond fair blue #> 2 Darth Vader 202 136 none white yellow #> 3 Leia Organa 150 49 brown light brown #> 4 Owen Lars 178 120 brown, grey light blue #> 5 Beru Whitesun lars 165 75 brown light blue #> 6 Biggs Darklighter 183 84 black light brown #> 7 Obi-Wan Kenobi 182 77 auburn, white fair blue-gray #> 8 Anakin Skywalker 188 84 blond fair blue #> 9 Wilhuff Tarkin 180 NA auburn, grey fair blue #> 10 Han Solo 180 80 brown fair brown #> # ... with 25 more rows, and 7 more variables: birth_year <dbl>, gender <chr>, #> # homeworld <chr>, species <chr>, films <list>, vehicles <list>, #> # starships <list>
filter(starwars, mass > 1000)
#> # A tibble: 1 × 13 #> name height mass hair_color skin_color eye_color #> <chr> <int> <dbl> <chr> <chr> <chr> #> 1 Jabba Desilijic Tiure 175 1358 <NA> green-tan, brown orange #> # ... with 7 more variables: birth_year <dbl>, gender <chr>, homeworld <chr>, #> # species <chr>, films <list>, vehicles <list>, starships <list>
# Multiple criteria filter(starwars, hair_color == "none" & eye_color == "black")
#> # A tibble: 9 × 13 #> name height mass hair_color skin_color eye_color birth_year #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> #> 1 Nien Nunb 160 68 none grey black NA #> 2 Gasgano 122 NA none white, blue black NA #> 3 Kit Fisto 196 87 none green black NA #> 4 Plo Koon 188 80 none orange black 22 #> 5 Lama Su 229 88 none grey black NA #> 6 Taun We 213 NA none grey black NA #> 7 Shaak Ti 178 57 none red, blue, white black NA #> 8 Tion Medon 206 80 none grey black NA #> 9 BB8 NA NA none none black NA #> # ... with 6 more variables: gender <chr>, homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>
filter(starwars, hair_color == "none" | eye_color == "black")
#> # A tibble: 38 × 13 #> name height mass hair_color skin_color eye_color birth_year #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> #> 1 Darth Vader 202 136 none white yellow 41.9 #> 2 Greedo 173 74 <NA> green black 44.0 #> 3 IG-88 200 140 none metal red 15.0 #> 4 Bossk 190 113 none green red 53.0 #> 5 Lobot 175 79 none light blue 37.0 #> 6 Ackbar 180 83 none brown mottle orange 41.0 #> 7 Nien Nunb 160 68 none grey black NA #> 8 Nute Gunray 191 90 none mottled green red NA #> 9 Jar Jar Binks 196 66 none orange orange 52.0 #> 10 Roos Tarpals 224 82 none grey orange NA #> # ... with 28 more rows, and 6 more variables: gender <chr>, homeworld <chr>, #> # species <chr>, films <list>, vehicles <list>, starships <list>
# Multiple arguments are equivalent to and filter(starwars, hair_color == "none", eye_color == "black")
#> # A tibble: 9 × 13 #> name height mass hair_color skin_color eye_color birth_year #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> #> 1 Nien Nunb 160 68 none grey black NA #> 2 Gasgano 122 NA none white, blue black NA #> 3 Kit Fisto 196 87 none green black NA #> 4 Plo Koon 188 80 none orange black 22 #> 5 Lama Su 229 88 none grey black NA #> 6 Taun We 213 NA none grey black NA #> 7 Shaak Ti 178 57 none red, blue, white black NA #> 8 Tion Medon 206 80 none grey black NA #> 9 BB8 NA NA none none black NA #> # ... with 6 more variables: gender <chr>, homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>