Classwork 9

Combining Tables with left_join()

Author

Byeong-Hak Choe

Published

October 16, 2026

NoteNote

Complete the questions and submit your responses on the corresponding Brightspace page. Fill in every ____. Copy the completed R code into your script and run it after the setup below.

These two small tables contain fictional student and major information. We want to keep every student and add the full name of each student’s major when a matching code exists.

library(dplyr)

students <- data.frame(
  student_id = 101:104,
  student_name = c("Ava", "Ben", "Cora", "Diego"),
  major_code = c("DANL", "BIOL", "DANL", "ART")
)

majors <- data.frame(
  major_code = c("BIOL", "DANL", "ECON"),
  major_name = c("Biology", "Data Analytics", "Economics")
)

students

student_id student_name major_code
101 Ava DANL
102 Ben BIOL
103 Cora DANL
104 Diego ART

majors

major_code major_name
BIOL Biology
DANL Data Analytics
ECON Economics

The rows appear in different orders. A join matches key values, not row positions.

Question 1

Which table is the main (left) table, which table supplies the major names, and which column is the matching key? Fill in the names below.

  • Main (left) table: ____
  • Lookup table supplying the major names: ____
  • Matching key column: ____



Question 2

Fill in the code to add major_name to the student table. Save the result as students_with_major and display it.

students_with_major <- ____ |>
  ____(____, by = "____")
students_with_major



Back to top