Use filter() find rows/cases where conditions are true. Unlike base subsetting with [, 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 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.

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

Examples

filter(starwars, species == "Human")
#> # A tibble: 35 x 13 #> name height mass hair_color skin_color eye_color birth_year gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 Luke Skywal… 172 77 blond fair blue 19 male #> 2 Darth Vader 202 136 none white yellow 41.9 male #> 3 Leia Organa 150 49 brown light brown 19 female #> 4 Owen Lars 178 120 brown, grey light blue 52 male #> 5 Beru Whites… 165 75 brown light blue 47 female #> 6 Biggs Darkl… 183 84 black light brown 24 male #> 7 Obi-Wan Ken… 182 77 auburn, wh… fair blue-gray 57 male #> 8 Anakin Skyw… 188 84 blond fair blue 41.9 male #> 9 Wilhuff Tar… 180 NA auburn, gr… fair blue 64 male #> 10 Han Solo 180 80 brown fair brown 29 male #> # ... with 25 more rows, and 5 more variables: homeworld <chr>, species <chr>, #> # films <list>, vehicles <list>, starships <list>
filter(starwars, mass > 1000)
#> # A tibble: 1 x 13 #> name height mass hair_color skin_color eye_color birth_year gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 Jabba Des… 175 1358 <NA> green-tan, … orange 600 hermaph… #> # ... with 5 more variables: homeworld <chr>, species <chr>, films <list>, #> # vehicles <list>, starships <list>
# Multiple criteria filter(starwars, hair_color == "none" & eye_color == "black")
#> # A tibble: 9 x 13 #> name height mass hair_color skin_color eye_color birth_year gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 Nien Nunb 160 68 none grey black NA male #> 2 Gasgano 122 NA none white, blue black NA male #> 3 Kit Fisto 196 87 none green black NA male #> 4 Plo Koon 188 80 none orange black 22 male #> 5 Lama Su 229 88 none grey black NA male #> 6 Taun We 213 NA none grey black NA female #> 7 Shaak Ti 178 57 none red, blue, wh… black NA female #> 8 Tion Medon 206 80 none grey black NA male #> 9 BB8 NA NA none none black NA none #> # ... with 5 more variables: homeworld <chr>, species <chr>, films <list>, #> # vehicles <list>, starships <list>
filter(starwars, hair_color == "none" | eye_color == "black")
#> # A tibble: 38 x 13 #> name height mass hair_color skin_color eye_color birth_year gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 Darth Vader 202 136 none white yellow 41.9 male #> 2 Greedo 173 74 <NA> green black 44 male #> 3 IG-88 200 140 none metal red 15 none #> 4 Bossk 190 113 none green red 53 male #> 5 Lobot 175 79 none light blue 37 male #> 6 Ackbar 180 83 none brown mottle orange 41 male #> 7 Nien Nunb 160 68 none grey black NA male #> 8 Nute Gunray 191 90 none mottled gre… red NA male #> 9 Jar Jar Bi… 196 66 none orange orange 52 male #> 10 Roos Tarpa… 224 82 none grey orange NA male #> # ... with 28 more rows, and 5 more variables: 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 x 13 #> name height mass hair_color skin_color eye_color birth_year gender #> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> #> 1 Nien Nunb 160 68 none grey black NA male #> 2 Gasgano 122 NA none white, blue black NA male #> 3 Kit Fisto 196 87 none green black NA male #> 4 Plo Koon 188 80 none orange black 22 male #> 5 Lama Su 229 88 none grey black NA male #> 6 Taun We 213 NA none grey black NA female #> 7 Shaak Ti 178 57 none red, blue, wh… black NA female #> 8 Tion Medon 206 80 none grey black NA male #> 9 BB8 NA NA none none black NA none #> # ... with 5 more variables: homeworld <chr>, species <chr>, films <list>, #> # vehicles <list>, starships <list>