A join combines two tabular data sets into one object by matching shared key variables. It is a core data-wrangling step for preparing complementary data for analysis and visualization. section 2.3 section 6.3
The matching variables must identify corresponding records. For NBA team data, Team and season connect records from nba and stats; converting playoffs to a factor prepares its categorical values before the join. section 14.2.5
Choose the join according to which records should remain:
- A left join keeps every row from the first data set and only matching rows from the second. section 14.2.5
- A right join switches the data sets; an outer or full join keeps every row from both. section 14.2.5
- A natural or inner join keeps only records that match in both data sets. section 14.2.5
Combining NBA records
Joining stats with nba by Team and season creates nba_stats with every unique column from both data sets, including wins, playoffs, PTS, O_PTS, and PTS_DIFF. In the demonstrated data, left, right, inner, and full joins produce the same displayed results because the records match. section 14.2.5
nba_stats <- left_join(stats, nba, by = c("Team", "season"))
Use dplyr left_join() to merge the NBA data sets by their shared Team and season variables. section 14.2.5















