liveBook · Manning

Join

As explained in Statistics Slam Dunk

A join operation combines rows from two tabular datasets according to matching values in one or more key variables.

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

How Statistics Slam Dunk covers it

How deep it goes
Defines four join types and demonstrates shared-key behavior, but does not develop join internals or unmatched-record diagnostics.
Examples
Uses NBA team-season data, the Team and season keys, playoffs conversion, dplyr joins, and head() output.
What it assumes
Assumes tabular data sets and shared variables, while situating joins within broader data-wrangling operations.
What you can do after
Apply it via dplyr to merge NBA and statistics data by multiple shared variables.
How it connects
Connects joining with data wrangling, subsetting, reshaping, factor conversion, statistical modeling, and visualization.

Also explained in

Compare how 16 books explain join →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.