Filtering rows

Author

Jeffrey R. Stevens

Published

February 20, 2023

For these exercises, we’ll use a new clean version of the dog breed traits data set.

  1. Import data from https://jeffreyrstevens.quarto.pub/dpavir/data/dog_breed_traits_clean.csv and assign to traits.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
traits <- read_csv("https://jeffreyrstevens.quarto.pub/dpavir/data/dog_breed_traits_clean.csv")
Rows: 197 Columns: 8
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): breed, coat_type, coat_length
dbl (5): affectionate, children, other_dogs, shedding, grooming

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  1. View dogs only with short coats.
filter(traits, coat_length == "Short")
# A tibble: 87 × 8
   breed            affectionate children other_dogs shedding grooming coat_type
   <chr>                   <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>    
 1 Retrievers (Lab…            5        5          5        4        2 Double   
 2 French Bulldogs             5        5          4        3        1 Smooth   
 3 Bulldogs                    4        3          3        3        3 Smooth   
 4 Beagles                     3        5          5        3        2 Smooth   
 5 Rottweilers                 5        3          3        3        1 Smooth   
 6 Pointers (Germa…            5        5          4        3        2 Smooth   
 7 Dachshunds                  5        3          4        2        2 Smooth   
 8 Pembroke Welsh …            5        3          4        4        2 Double   
 9 Boxers                      4        5          3        2        2 Smooth   
10 Great Danes                 5        3          3        3        1 Smooth   
# ℹ 77 more rows
# ℹ 1 more variable: coat_length <chr>
  1. View a data frame excluding dogs with short coats.
filter(traits, coat_length != "Short")
# A tibble: 109 × 8
   breed            affectionate children other_dogs shedding grooming coat_type
   <chr>                   <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>    
 1 German Shepherd…            5        5          3        4        2 Double   
 2 Retrievers (Gol…            5        5          5        4        2 Double   
 3 Poodles                     5        5          3        1        4 Curly    
 4 Australian Shep…            3        5          3        3        2 Double   
 5 Yorkshire Terri…            5        5          3        1        5 Silky    
 6 Siberian Huskies            5        5          5        4        2 Double   
 7 Cavalier King C…            5        5          5        2        2 Wavy     
 8 Miniature Schna…            5        5          3        3        4 Wiry     
 9 Shih Tzu                    5        5          5        1        4 Double   
10 Bernese Mountai…            5        5          5        5        3 Double   
# ℹ 99 more rows
# ℹ 1 more variable: coat_length <chr>
  1. View dogs with double or silky coats.
filter(traits, coat_type %in% c("Double", "Silky"))
# A tibble: 75 × 8
   breed            affectionate children other_dogs shedding grooming coat_type
   <chr>                   <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>    
 1 Retrievers (Lab…            5        5          5        4        2 Double   
 2 German Shepherd…            5        5          3        4        2 Double   
 3 Retrievers (Gol…            5        5          5        4        2 Double   
 4 Pembroke Welsh …            5        3          4        4        2 Double   
 5 Australian Shep…            3        5          3        3        2 Double   
 6 Yorkshire Terri…            5        5          3        1        5 Silky    
 7 Siberian Huskies            5        5          5        4        2 Double   
 8 Shih Tzu                    5        5          5        1        4 Double   
 9 Bernese Mountai…            5        5          5        5        3 Double   
10 Pomeranians                 5        3          3        2        3 Double   
# ℹ 65 more rows
# ℹ 1 more variable: coat_length <chr>
  1. View dogs with double or silky coats and shedding ratings 3 or below.
filter(traits, coat_type %in% c("Double", "Silky") & shedding <= 3)
# A tibble: 65 × 8
   breed            affectionate children other_dogs shedding grooming coat_type
   <chr>                   <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>    
 1 Australian Shep…            3        5          3        3        2 Double   
 2 Yorkshire Terri…            5        5          3        1        5 Silky    
 3 Shih Tzu                    5        5          5        1        4 Double   
 4 Pomeranians                 5        3          3        2        3 Double   
 5 Havanese                    5        5          5        2        3 Double   
 6 Spaniels (Engli…            5        3          4        3        2 Double   
 7 Shetland Sheepd…            5        5          5        3        3 Double   
 8 Brittanys                   3        4          4        3        3 Double   
 9 Spaniels (Cocke…            4        5          5        3        4 Double   
10 Miniature Ameri…            5        5          5        3        3 Double   
# ℹ 55 more rows
# ℹ 1 more variable: coat_length <chr>
  1. View dogs with NA for coat_type.
filter(traits, is.na(coat_type))
# A tibble: 1 × 8
  breed affectionate children other_dogs shedding grooming coat_type coat_length
  <chr>        <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>     <chr>      
1 Engl…            4        5          5        3        2 <NA>      <NA>       
  1. View dogs with NA for any column.
filter(traits, if_any(everything(), is.na))
# A tibble: 2 × 8
  breed affectionate children other_dogs shedding grooming coat_type coat_length
  <chr>        <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>     <chr>      
1 Amer…            5        2          4        5       NA Rough     Medium     
2 Engl…            4        5          5        3        2 <NA>      <NA>       
  1. View dogs not missing any data.
drop_na(traits)
# A tibble: 195 × 8
   breed            affectionate children other_dogs shedding grooming coat_type
   <chr>                   <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>    
 1 Retrievers (Lab…            5        5          5        4        2 Double   
 2 French Bulldogs             5        5          4        3        1 Smooth   
 3 German Shepherd…            5        5          3        4        2 Double   
 4 Retrievers (Gol…            5        5          5        4        2 Double   
 5 Bulldogs                    4        3          3        3        3 Smooth   
 6 Poodles                     5        5          3        1        4 Curly    
 7 Beagles                     3        5          5        3        2 Smooth   
 8 Rottweilers                 5        3          3        3        1 Smooth   
 9 Pointers (Germa…            5        5          4        3        2 Smooth   
10 Dachshunds                  5        3          4        2        2 Smooth   
# ℹ 185 more rows
# ℹ 1 more variable: coat_length <chr>
  1. View dogs sorted by breed name.
arrange(traits, breed)
# A tibble: 197 × 8
   breed            affectionate children other_dogs shedding grooming coat_type
   <chr>                   <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>    
 1 Affenpinschers              3        3          3        3        3 Wiry     
 2 Afghan Hounds               3        3          3        1        4 Silky    
 3 Airedale Terrie…            3        3          3        1        3 Wiry     
 4 Akitas                      3        3          1        3        3 Double   
 5 Alaskan Malamut…            3        3          3        3        3 Double   
 6 American Englis…            3        3          5        2        1 Smooth   
 7 American Eskimo…            5        5          3        3        3 Double   
 8 American Foxhou…            3        5          5        3        1 Smooth   
 9 American Hairle…            5        5          3        1        1 Hairless 
10 American Rearsn…            5        2          4        5       NA Rough    
# ℹ 187 more rows
# ℹ 1 more variable: coat_length <chr>
  1. View dogs sorted by coat type then coat length then affectionate rating.
arrange(traits, coat_type, coat_length, affectionate)
# A tibble: 197 × 8
   breed            affectionate children other_dogs shedding grooming coat_type
   <chr>                   <dbl>    <dbl>      <dbl>    <dbl>    <dbl> <chr>    
 1 Bergamasco Shee…            3        3          3        1        1 Corded   
 2 Pulik                       5        3          3        1        5 Corded   
 3 Komondorok                  5        3          2        1        4 Corded   
 4 Spanish Water D…            5        4          3        1        4 Corded   
 5 Poodles                     5        5          3        1        4 Curly    
 6 Portuguese Wate…            5        5          4        2        4 Curly    
 7 Borzois                     3        3          3        3        2 Curly    
 8 Bedlington Terr…            3        3          3        1        3 Curly    
 9 Barbets                     4        5          5        1        3 Curly    
10 Pumik                       5        3          3        1        2 Curly    
# ℹ 187 more rows
# ℹ 1 more variable: coat_length <chr>