merge()
In this page:
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()
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)
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: