Joining Data Activity






Grayson White

Math 241
Week 4 | Spring 2026

Loading data

We’ll create the x and y data frames in R:

library(tidyverse)
x <- data.frame(spp_code = c("ABBA", "BEPA", "PIST"),
                common_name = c("balsum fir", "paper birch", "white pine"))

y <- data.frame(spp_code = c("ABBA", "BEPA", "LALA"),
                latin_name = c("Abies balsamea", 
                               "Betula papyrifera",
                               "Larix laricina"))

Loading data

We’ll create the x and y data frames in R:

x
  spp_code common_name
1     ABBA  balsum fir
2     BEPA paper birch
3     PIST  white pine
y
  spp_code        latin_name
1     ABBA    Abies balsamea
2     BEPA Betula papyrifera
3     LALA    Larix laricina

Q1. Write the code to get this output

  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera
3     PIST  white pine              <NA>
01:00

Q1. Write the code to get this output

left_join(x, y, by = "spp_code")
  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera
3     PIST  white pine              <NA>

Q2. Write the code to get this output

  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera
3     PIST  white pine              <NA>
4     LALA        <NA>    Larix laricina
01:00

Q2. Write the code to get this output

full_join(x, y, by = "spp_code")
  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera
3     PIST  white pine              <NA>
4     LALA        <NA>    Larix laricina

Q3. Write the code to get this output

  spp_code        latin_name common_name
1     ABBA    Abies balsamea  balsum fir
2     BEPA Betula papyrifera paper birch
3     LALA    Larix laricina        <NA>
01:00

Q3. Write the code to get this output

left_join(y, x, by = "spp_code")
  spp_code        latin_name common_name
1     ABBA    Abies balsamea  balsum fir
2     BEPA Betula papyrifera paper birch
3     LALA    Larix laricina        <NA>

Q4. Write the code to get this output

  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera
3     LALA        <NA>    Larix laricina
01:00

Q4. Write the code to get this output

right_join(x, y, by = "spp_code")
  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera
3     LALA        <NA>    Larix laricina

Q5. Write the code to get this output

  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera
01:00

Q5. Write the code to get this output

inner_join(x, y, by = "spp_code")
  spp_code common_name        latin_name
1     ABBA  balsum fir    Abies balsamea
2     BEPA paper birch Betula papyrifera

Q6. Write the code to get this output

  spp_code        latin_name common_name
1     ABBA    Abies balsamea  balsum fir
2     BEPA Betula papyrifera paper birch
3     LALA    Larix laricina        <NA>
4     PIST              <NA>  white pine
01:00

Q6. Write the code to get this output

full_join(y, x, by = "spp_code")
  spp_code        latin_name common_name
1     ABBA    Abies balsamea  balsum fir
2     BEPA Betula papyrifera paper birch
3     LALA    Larix laricina        <NA>
4     PIST              <NA>  white pine

Consider a third data frame, z:

z <- x %>%
  rename(species_code = spp_code)
z
  species_code common_name
1         ABBA  balsum fir
2         BEPA paper birch
3         PIST  white pine

Q7. Write the code to get this output

  species_code common_name        latin_name
1         ABBA  balsum fir    Abies balsamea
2         BEPA paper birch Betula papyrifera
3         PIST  white pine              <NA>
01:00

Q7. Write the code to get this output

left_join(z, y, by = c("species_code" = "spp_code"))
  species_code common_name        latin_name
1         ABBA  balsum fir    Abies balsamea
2         BEPA paper birch Betula papyrifera
3         PIST  white pine              <NA>

Q8. Write the code to get this output (hint: use a filtering join)

  spp_code        latin_name
1     ABBA    Abies balsamea
2     BEPA Betula papyrifera
01:00

Q8. Write the code to get this output (hint: use a filtering join)

semi_join(y, x, by = "spp_code")
  spp_code        latin_name
1     ABBA    Abies balsamea
2     BEPA Betula papyrifera

Q9. Write the code to get this output (hint: use a filtering join)

  species_code common_name
1         PIST  white pine
01:00

Q9. Write the code to get this output (hint: use a filtering join)

anti_join(z, y, by = c("species_code" = "spp_code"))
  species_code common_name
1         PIST  white pine

Q10. Write the code to get this output (hint: use a filtering join)

  spp_code     latin_name
1     LALA Larix laricina
01:00

Q10. Write the code to get this output (hint: use a filtering join)

anti_join(y, x, by = "spp_code")
  spp_code     latin_name
1     LALA Larix laricina
# or 
anti_join(y, z, by = c("spp_code" = "species_code"))
  spp_code     latin_name
1     LALA Larix laricina