← Back to R Course | Chapter 8: Data Manipulation | Lesson 4 of 7

merge()

In this page:

  1. merge()

merge()

merge(df1, df2, by = "key") joins two data frames together based on a shared column, similar to a SQL join. By default it performs an inner join, keeping only rows with matching keys in both frames; all = TRUE performs a full outer join instead. This is the standard way to combine related data spread across multiple tables.

Note: Set all.x = TRUE for a left join (keep every row from the first data frame, matched or not).

Example: merge()

markup
people <- data.frame(id = c(1, 2, 3), name = c("Alice", "Bob", "Cara"))
scores <- data.frame(id = c(1, 2), score = c(95, 82))

merged <- merge(people, scores, by = "id")
print(merged)
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.