Verb filter()
picks cases based on their values (it’s a filter on rows of the data frame).
starwars %>%
filter(species == "Droid")
## # A tibble: 6 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 C-3PO 167 75 <NA> gold yellow 112 none masculi…
## 2 R2-D2 96 32 <NA> white, blue red 33 none masculi…
## 3 R5-D4 97 32 <NA> white, red red NA none masculi…
## 4 IG-88 200 140 none metal red 15 none masculi…
## 5 R4-P17 96 NA none silver, red red, blue NA none feminine
## 6 BB8 NA NA none none black NA none masculi…
## # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
starwars %>%
filter(species == "Droid" & eye_color == "red")
## # A tibble: 3 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 R2-D2 96 32 <NA> white, blue red 33 none masculine
## 2 R5-D4 97 32 <NA> white, red red NA none masculine
## 3 IG-88 200 140 none metal red 15 none masculine
## # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
# or
starwars %>%
filter(species == "Droid", eye_color == "red")
## # A tibble: 3 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 R2-D2 96 32 <NA> white, blue red 33 none masculine
## 2 R5-D4 97 32 <NA> white, red red NA none masculine
## 3 IG-88 200 140 none metal red 15 none masculine
## # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
starwars %>%
filter(species == "Droid" | species == "Wookiee")
## # A tibble: 8 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 C-3PO 167 75 <NA> gold yellow 112 none mascu…
## 2 R2-D2 96 32 <NA> white, blue red 33 none mascu…
## 3 R5-D4 97 32 <NA> white, red red NA none mascu…
## 4 Chewbacca 228 112 brown unknown blue 200 male mascu…
## 5 IG-88 200 140 none metal red 15 none mascu…
## 6 R4-P17 96 NA none silver, red red, blue NA none femin…
## 7 Tarfful 234 136 brown brown blue NA male mascu…
## 8 BB8 NA NA none none black NA none mascu…
## # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>
starwars %>%
filter(species == "Droid", !is.na(birth_year))
## # A tibble: 3 × 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 C-3PO 167 75 <NA> gold yellow 112 none masculine
## 2 R2-D2 96 32 <NA> white, blue red 33 none masculine
## 3 IG-88 200 140 none metal red 15 none masculine
## # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
## # vehicles <list>, starships <list>