filter: Return rows with matching conditions

Description Usage Arguments Details Value Useful filter functions Tidy data Scoped filtering See Also Examples

View source: R/manip.r

Description

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

Usage

1

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

Examples

1
2
3
4
5
6
7
8
9
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")

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

# A tibble: 35 x 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>
# A tibble: 1 x 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>
# A tibble: 9 x 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>
# A tibble: 38 x 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>
# A tibble: 9 x 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>

dplyr documentation built on Nov. 10, 2018, 9:04 a.m.