1. Import your data

Import two related datasets from TidyTuesday Project.

breed <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/refs/heads/main/data/2022/2022-02-01/breed_rank.csv')
## Rows: 195 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Breed, links, Image
## dbl (8): 2013 Rank, 2014 Rank, 2015 Rank, 2016 Rank, 2017 Rank, 2018 Rank, 2...
## 
## ℹ 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.
traits <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/refs/heads/main/data/2022/2022-02-01/breed_traits.csv')
## Rows: 195 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): Breed, Coat Type, Coat Length
## dbl (14): Affectionate With Family, Good With Young Children, Good With Othe...
## 
## ℹ 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.

2. Make data small

Describe the two datasets:

Data 1 (Breed)

Data 2 (Traits)

set.seed(1234)
breed_small <- breed %>% select(Breed, `2020 Rank`, `Retrievers (Labrador)` = contains("Rank")) %>% sample_n(10)
traits_small <- traits %>% select(Breed, `Affectionate With Family`, `Good With Young Children`) %>% sample_n(10)

3. inner_join

Describe the resulting data:

How is it different from the original two datasets? Only keeps rows that have an exact match in both tables based on Breed.

breed_small %>% inner_join(traits_small, by = "Breed")
## # A tibble: 0 × 11
## # ℹ 11 variables: Breed <chr>, Retrievers (Labrador)8 <dbl>,
## #   Retrievers (Labrador)1 <dbl>, Retrievers (Labrador)2 <dbl>,
## #   Retrievers (Labrador)3 <dbl>, Retrievers (Labrador)4 <dbl>,
## #   Retrievers (Labrador)5 <dbl>, Retrievers (Labrador)6 <dbl>,
## #   Retrievers (Labrador)7 <dbl>, Affectionate With Family <dbl>,
## #   Good With Young Children <dbl>

4. left_join

Describe the resulting data:

How is it different from the original two datasets? It preserves all rows from left table (Breed)

breed_small %>% left_join(traits_small, by = "Breed")
## # A tibble: 10 × 11
##    Breed    Retrievers (Labrador…¹ Retrievers (Labrador…² Retrievers (Labrador…³
##    <chr>                     <dbl>                  <dbl>                  <dbl>
##  1 Brittan…                     28                     30                     27
##  2 Coton d…                     80                     NA                     NA
##  3 Briards                     150                    127                    132
##  4 Setters…                    101                     91                     89
##  5 Japanes…                    111                     87                     93
##  6 Bluetic…                    137                    128                    130
##  7 Kerry B…                    133                    126                    128
##  8 Scottis…                    166                    165                    157
##  9 Salukis                     144                    115                    134
## 10 Manches…                    132                    120                    136
## # ℹ abbreviated names: ¹​`Retrievers (Labrador)8`, ²​`Retrievers (Labrador)1`,
## #   ³​`Retrievers (Labrador)2`
## # ℹ 7 more variables: `Retrievers (Labrador)3` <dbl>,
## #   `Retrievers (Labrador)4` <dbl>, `Retrievers (Labrador)5` <dbl>,
## #   `Retrievers (Labrador)6` <dbl>, `Retrievers (Labrador)7` <dbl>,
## #   `Affectionate With Family` <dbl>, `Good With Young Children` <dbl>

5. right_join

Describe the resulting data:

How is it different from the original two datasets? Retains all rows from the right table (Traits)

breed_small %>% right_join(traits_small, by = "Breed")
## # A tibble: 10 × 11
##    Breed    Retrievers (Labrador…¹ Retrievers (Labrador…² Retrievers (Labrador…³
##    <chr>                     <dbl>                  <dbl>                  <dbl>
##  1 Leonber…                     NA                     NA                     NA
##  2 Borzois                      NA                     NA                     NA
##  3 Retriev…                     NA                     NA                     NA
##  4 Great P…                     NA                     NA                     NA
##  5 Chinese…                     NA                     NA                     NA
##  6 Pointers                     NA                     NA                     NA
##  7 Boxers                       NA                     NA                     NA
##  8 Black R…                     NA                     NA                     NA
##  9 Canaan …                     NA                     NA                     NA
## 10 Airedal…                     NA                     NA                     NA
## # ℹ abbreviated names: ¹​`Retrievers (Labrador)8`, ²​`Retrievers (Labrador)1`,
## #   ³​`Retrievers (Labrador)2`
## # ℹ 7 more variables: `Retrievers (Labrador)3` <dbl>,
## #   `Retrievers (Labrador)4` <dbl>, `Retrievers (Labrador)5` <dbl>,
## #   `Retrievers (Labrador)6` <dbl>, `Retrievers (Labrador)7` <dbl>,
## #   `Affectionate With Family` <dbl>, `Good With Young Children` <dbl>

6. full_join

Describe the resulting data:

How is it different from the original two datasets? Combines every row from both tables.

breed_small %>% full_join(traits_small, by = "Breed")
## # A tibble: 20 × 11
##    Breed    Retrievers (Labrador…¹ Retrievers (Labrador…² Retrievers (Labrador…³
##    <chr>                     <dbl>                  <dbl>                  <dbl>
##  1 Brittan…                     28                     30                     27
##  2 Coton d…                     80                     NA                     NA
##  3 Briards                     150                    127                    132
##  4 Setters…                    101                     91                     89
##  5 Japanes…                    111                     87                     93
##  6 Bluetic…                    137                    128                    130
##  7 Kerry B…                    133                    126                    128
##  8 Scottis…                    166                    165                    157
##  9 Salukis                     144                    115                    134
## 10 Manches…                    132                    120                    136
## 11 Leonber…                     NA                     NA                     NA
## 12 Borzois                      NA                     NA                     NA
## 13 Retriev…                     NA                     NA                     NA
## 14 Great P…                     NA                     NA                     NA
## 15 Chinese…                     NA                     NA                     NA
## 16 Pointers                     NA                     NA                     NA
## 17 Boxers                       NA                     NA                     NA
## 18 Black R…                     NA                     NA                     NA
## 19 Canaan …                     NA                     NA                     NA
## 20 Airedal…                     NA                     NA                     NA
## # ℹ abbreviated names: ¹​`Retrievers (Labrador)8`, ²​`Retrievers (Labrador)1`,
## #   ³​`Retrievers (Labrador)2`
## # ℹ 7 more variables: `Retrievers (Labrador)3` <dbl>,
## #   `Retrievers (Labrador)4` <dbl>, `Retrievers (Labrador)5` <dbl>,
## #   `Retrievers (Labrador)6` <dbl>, `Retrievers (Labrador)7` <dbl>,
## #   `Affectionate With Family` <dbl>, `Good With Young Children` <dbl>

7. semi_join

Describe the resulting data:

How is it different from the original two datasets? Filters the dataset to keep rows with a corresponding match in the second dataset.

breed_small %>% semi_join(traits_small, by = "Breed")
## # A tibble: 0 × 9
## # ℹ 9 variables: Breed <chr>, Retrievers (Labrador)8 <dbl>,
## #   Retrievers (Labrador)1 <dbl>, Retrievers (Labrador)2 <dbl>,
## #   Retrievers (Labrador)3 <dbl>, Retrievers (Labrador)4 <dbl>,
## #   Retrievers (Labrador)5 <dbl>, Retrievers (Labrador)6 <dbl>,
## #   Retrievers (Labrador)7 <dbl>

8. anti_join

Describe the resulting data:

How is it different from the original two datasets? Filters our any rows from the first dataset that does have a match. Leaves only unique rows.

breed_small %>% anti_join(traits_small, by = "Breed")
## # A tibble: 10 × 9
##    Breed    Retrievers (Labrador…¹ Retrievers (Labrador…² Retrievers (Labrador…³
##    <chr>                     <dbl>                  <dbl>                  <dbl>
##  1 Brittan…                     28                     30                     27
##  2 Coton d…                     80                     NA                     NA
##  3 Briards                     150                    127                    132
##  4 Setters…                    101                     91                     89
##  5 Japanes…                    111                     87                     93
##  6 Bluetic…                    137                    128                    130
##  7 Kerry B…                    133                    126                    128
##  8 Scottis…                    166                    165                    157
##  9 Salukis                     144                    115                    134
## 10 Manches…                    132                    120                    136
## # ℹ abbreviated names: ¹​`Retrievers (Labrador)8`, ²​`Retrievers (Labrador)1`,
## #   ³​`Retrievers (Labrador)2`
## # ℹ 5 more variables: `Retrievers (Labrador)3` <dbl>,
## #   `Retrievers (Labrador)4` <dbl>, `Retrievers (Labrador)5` <dbl>,
## #   `Retrievers (Labrador)6` <dbl>, `Retrievers (Labrador)7` <dbl>