1. Import your data

Import two related datasets from TidyTuesday Project.

tt_datasets <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-07-02/tt_datasets.csv")
## Rows: 644 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): dataset_name
## dbl (4): year, week, variables, observations
## 
## ℹ 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.
tt_variables <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2024/2024-07-02/tt_variables.csv")
## Rows: 7897 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): dataset_name, variable, class, min, max, description
## dbl (3): year, week, n_unique
## 
## ℹ 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:

Data1

Data 2

ds_small <- tt_datasets %>%
  select(dataset_name, year, observations) %>%
  distinct(dataset_name, .keep_all = TRUE) %>%
  slice(1:4)

var_small <- tt_variables %>%
  select(dataset_name, variable, class) %>%
  slice(1:5)

dim(ds_small)
## [1] 4 3
dim(var_small)
## [1] 5 3

3. inner_join

Describe the resulting data:

How is it different from the original two datasets?

It keeps only matching dataset_name rows found in both small tables and combines their columns side by side, dropping unmatched rows from both.

inner_join(ds_small, var_small, by = "dataset_name")
## # A tibble: 5 × 5
##   dataset_name    year observations variable class    
##   <chr>          <dbl>        <dbl> <chr>    <chr>    
## 1 us_avg_tuition  2018           50 State    character
## 2 us_avg_tuition  2018           50 2004-05  double   
## 3 us_avg_tuition  2018           50 2005-06  double   
## 4 us_avg_tuition  2018           50 2006-07  double   
## 5 us_avg_tuition  2018           50 2007-08  double

4. left_join

Describe the resulting data:

How is it different from the original two datasets?

It keeps every row from ds_small even when there is no match in var_small, while dropping unmatched rows from var_small.

left_join(ds_small, var_small, by = "dataset_name")
## # A tibble: 8 × 5
##   dataset_name             year observations variable class    
##   <chr>                   <dbl>        <dbl> <chr>    <chr>    
## 1 us_avg_tuition           2018           50 State    character
## 2 us_avg_tuition           2018           50 2004-05  double   
## 3 us_avg_tuition           2018           50 2005-06  double   
## 4 us_avg_tuition           2018           50 2006-07  double   
## 5 us_avg_tuition           2018           50 2007-08  double   
## 6 nfl_salary               2018          800 <NA>     <NA>     
## 7 global_mortality         2018         6156 <NA>     <NA>     
## 8 week4_australian_salary  2018         2197 <NA>     <NA>

5. right_join

Describe the resulting data:

How is it different from the original two datasets?

It keeps every row from var_small and fills NA for ds_small columns when dataset_name is missing from ds_small, dropping unmatched ds_small rows.

right_join(ds_small, var_small, by = "dataset_name")
## # A tibble: 5 × 5
##   dataset_name    year observations variable class    
##   <chr>          <dbl>        <dbl> <chr>    <chr>    
## 1 us_avg_tuition  2018           50 State    character
## 2 us_avg_tuition  2018           50 2004-05  double   
## 3 us_avg_tuition  2018           50 2005-06  double   
## 4 us_avg_tuition  2018           50 2006-07  double   
## 5 us_avg_tuition  2018           50 2007-08  double

6. full_join

Describe the resulting data:

How is it different from the original two datasets?

It keeps every row from both small tables and uses NA where a dataset_name only appears in one table.

full_join(ds_small, var_small, by = "dataset_name")
## # A tibble: 8 × 5
##   dataset_name             year observations variable class    
##   <chr>                   <dbl>        <dbl> <chr>    <chr>    
## 1 us_avg_tuition           2018           50 State    character
## 2 us_avg_tuition           2018           50 2004-05  double   
## 3 us_avg_tuition           2018           50 2005-06  double   
## 4 us_avg_tuition           2018           50 2006-07  double   
## 5 us_avg_tuition           2018           50 2007-08  double   
## 6 nfl_salary               2018          800 <NA>     <NA>     
## 7 global_mortality         2018         6156 <NA>     <NA>     
## 8 week4_australian_salary  2018         2197 <NA>     <NA>

7. semi_join

Describe the resulting data:

How is it different from the original two datasets?

It filters ds_small using var_small without adding columns from var_small or duplicating rows in ds_small.

semi_join(ds_small, var_small, by = "dataset_name")
## # A tibble: 1 × 3
##   dataset_name    year observations
##   <chr>          <dbl>        <dbl>
## 1 us_avg_tuition  2018           50

8. anti_join

Describe the resulting data:

How is it different from the original two datasets?

It keeps only the rows from ds_small that have no match in var_small, without adding columns from var_small.

anti_join(ds_small, var_small, by = "dataset_name")
## # A tibble: 3 × 3
##   dataset_name             year observations
##   <chr>                   <dbl>        <dbl>
## 1 nfl_salary               2018          800
## 2 global_mortality         2018         6156
## 3 week4_australian_salary  2018         2197