Mar 14, 2018
|
7 min
|
17.6k views
How to Join Two Data Frames in R: bind_rows, Set Operations and dplyr Joins (Part 4)
To combine two data frames in R: use bind_rows to stack them, bind_cols to paste one on as extra columns, and a join when the rows have to be matched on a shared key. left_join keeps every row in the first table, inner_join keeps only the rows that appear in both, right_join and outer_join do the mirror cases, and semi_join and anti_join filter one table by what is in the other.
What you will learn
When to use bind_rows, and why bind_cols needs care about row order What union, intersect and setdiff do to two data frames What a join key is, and what happens to rows that have no match The difference between left_join, inner_join, right_join and outer_join How semi_join and anti_join filter rather than combine How to check a join before you rely on it Chapters 0:00 What this series covers, and where part 4 fits 0:27 Stacking data with bind_rows 0:55 Adding columns with bind_cols, and when not to 1:50 Set operations: union, intersect and setdiff 2:18 What joining data actually means 2:46 left_join: keep every row in the first table 4:09 inner_join: keep only the rows in both 4:36 right_join and outer_join 5:32 semi_join and anti_join for filtering 6:27 The cheat sheet and the rest of the series
The rest of the series Part 1, what is data wrangling: https://www.youtube.com/watch?v=jOd65mR1zfw Part 2, tidy data and tidyr: https://www.youtube.com/watch?v=1ELALQlO-yM Part 3, dplyr: https://www.youtube.com/watch?v=Zc_ufg4uW4U
Learn more dplyr: https://dplyr.tidyverse.org Posit cheat sheets: https://posit.co/resources/cheatsheets/ R for Data Science: https://r4ds.hadley.nz
cheatsheets
dplyr
tidyr
tidyverse
tidyverse.org
Left Join in R
Inner Join R
Dplyr Join
Rbind in R
Bind_rows
How to Join Two Data Frames in R
Merge Data in R
Semi Join Anti Join
Set Operations R
Dplyr
Tidyverse
Data Wrangling in R
Posit