Working with multiple data frames

Lecture 5

Dr. Benjamin Soltoff

Cornell University
INFO 5001 - Fall 2024

September 12, 2024

Announcements

Announcements

fill vs. color

  • Use fill when a geom is filled
  • Use color when a geom is outlined

Relational joins

Introduction to relational data

  • Multiple tables of data that when combined together accomplish goals
  • Relations define the important element, not just the individual tables
  • Relations are defined between a pair of tables
  • Relational verbs
    • Mutating joins
    • Filtering joins

Comic book characters

Name Alignment Gender Publisher
Deadpool Chaotic Male Marvel
Batman Good Male DC
Sabrina Good Female Archie Comics

Publishers

Publisher Year founded
DC 1934
Marvel 1939
Image 1992

Mutating joins

inner_join()

inner_join()

inner_join(x = superheroes, y = publishers, by = join_by(Publisher))
# A tibble: 2 × 5
  Name     Alignment Gender Publisher `Year\nfounded`
  <chr>    <chr>     <chr>  <chr>               <dbl>
1 Deadpool Chaotic   Male   Marvel               1939
2 Batman   Good      Male   DC                   1934

left_join()

left_join()

left_join(x = superheroes, y = publishers, by = join_by(Publisher))
# A tibble: 3 × 5
  Name     Alignment Gender Publisher        `Year\nfounded`
  <chr>    <chr>     <chr>  <chr>                      <dbl>
1 Deadpool Chaotic   Male   "Marvel"                    1939
2 Batman   Good      Male   "DC"                        1934
3 Sabrina  Good      Female "Archie\nComics"              NA

right_join()

right_join()

right_join(x = superheroes, y = publishers, by = join_by(Publisher))
# A tibble: 3 × 5
  Name     Alignment Gender Publisher `Year\nfounded`
  <chr>    <chr>     <chr>  <chr>               <dbl>
1 Deadpool Chaotic   Male   Marvel               1939
2 Batman   Good      Male   DC                   1934
3 <NA>     <NA>      <NA>   Image                1992

right_join() reversed

full_join()

full_join()

full_join(x = superheroes, y = publishers, by = join_by(Publisher))
# A tibble: 4 × 5
  Name     Alignment Gender Publisher        `Year\nfounded`
  <chr>    <chr>     <chr>  <chr>                      <dbl>
1 Deadpool Chaotic   Male   "Marvel"                    1939
2 Batman   Good      Male   "DC"                        1934
3 Sabrina  Good      Female "Archie\nComics"              NA
4 <NA>     <NA>      <NA>   "Image"                     1992

Filtering joins

semi_join()

semi_join()

semi_join(x = superheroes, y = publishers, by = join_by(Publisher))
# A tibble: 2 × 4
  Name     Alignment Gender Publisher
  <chr>    <chr>     <chr>  <chr>    
1 Deadpool Chaotic   Male   Marvel   
2 Batman   Good      Male   DC       

anti_join()

anti_join()

anti_join(x = superheroes, y = publishers, by = join_by(Publisher))
# A tibble: 1 × 4
  Name    Alignment Gender Publisher       
  <chr>   <chr>     <chr>  <chr>           
1 Sabrina Good      Female "Archie\nComics"

Application exercise

Goal

Join data from multiple data frames, summarize it, and create this plot.

ae-03

  • Go to the course GitHub org and find your ae-03 (repo name will be suffixed with your GitHub name).
  • Clone the repo in RStudio, run renv::restore() to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.
  • Render, commit, and push your edits by the AE deadline – end of the day

Recap of AE

  • Use the *_join() function appropriate for your analysis
  • Leverage the difference between mutating and filtering joins

TidyTuesday

A logo for Tidy Tuesday. Describes Tidy Tuesday as a weekly data project from the Data Science Learning Community.