Lab 11 : Joining Data Frames

Learning objectives

  • Keys
  • Basic joins
  • Filtering joins
  • Non-equi joins

Overview

Load the tidyverse

R code
library(tidyverse)

Common Join Functions

dplyr offers several types of joins:

  • inner_join(x, y, by): Keeps only rows with matching keys in both data frames.
  • left_join(x, y, by): Keeps all rows from x and adds matching data from y.
  • right_join(x, y, by): Keeps all rows from y and adds matching data from x.
  • full_join(x, y, by): Keeps all rows from both data frames.
  • semi_join(x, y, by): Keeps rows from x that have a match in y (no columns from y).
  • anti_join(x, y, by): Keeps rows from x that do not have a match in y.

Example Dat

R code
species <- tibble(id = c(1, 2, 3),
              name = c("Species1", "Species2", "Species3"))

abundance <- tibble(id = c(2, 3, 4),
              abundance = c(85, 90, 95))

Perform Joins

Left Join

R code
left_join(species, abundance, by = "id")
# A tibble: 3 × 3
     id name     abundance
  <dbl> <chr>        <dbl>
1     1 Species1        NA
2     2 Species2        85
3     3 Species3        90

Result: All rows from species, scores added where available.

Inner Join

R code
inner_join(species, abundance, by = "id")
# A tibble: 2 × 3
     id name     abundance
  <dbl> <chr>        <dbl>
1     2 Species2        85
2     3 Species3        90

Result: Only rows with id 2 and 3.

Full Join

R code
full_join(species, abundance, by = "id")
# A tibble: 4 × 3
     id name     abundance
  <dbl> <chr>        <dbl>
1     1 Species1        NA
2     2 Species2        85
3     3 Species3        90
4     4 <NA>            95

Result: All rows from both, with NA where no match.

Notes

  • If column names differ, use by = c("col_in_x" = "col_in_y").
  • Joins preserve the order of the first data frame.

Exercises

Today in preparation for joining data frames with NEON data we will walk through Chapter 19 Joins in R for Data Science. As we did earlier, by putting the examples and exercises in our own Quarto Markdown file, we can create own personal path through the Chapter.

What to upload to Canvas

After you Render the qmd file to an html file, export the file to your computer and upload it to Canvas.