liveBook · Manning

Rename() function

As explained in Statistics Slam Dunk

The dplyr rename() function changes column names in a tabular data object while preserving the associated data.

Use dplyr's rename() to change column names without changing the associated data. Write the new name on the left and the existing name on the right, as in rename(teamR = TEAM). This is especially useful when similar road and home columns will later be combined. section 6.3.2 section 7.3

Give road and home columns distinct names

After filtering NBA games into road and home data sets, rename shared fields before joining them. Road columns become teamR, venueR, Q1R, and FR; home columns become teamH, venueH, Q1H, and FH. Keep dataset, ID, and date unchanged so left_join() can match corresponding games. section 6.3.2

road %>%
 rename(dataset = DATASET, ID = GAME_ID, date = DATE, 
        teamR = TEAM, venueR = VENUE, Q1R = Q1, Q2R = Q2, 
        Q3R = Q3, Q4R = Q4, OT1R = OT1,       
        OT2R = OT2, OT3R = OT3, OT4R = OT4, 
        OT5R = OT5, FR = F, MINR = MIN) -> road
home %>%
 rename(dataset = DATASET, ID = GAME_ID, date = DATE, 
        teamH = TEAM, venueH = VENUE, Q1H = Q1, Q2H = Q2,
        Q3H = Q3, Q4H = Q4, OT1H = OT1,       
        OT2H = OT2, OT3H = OT3, OT4H = OT4, 
        OT5H = OT5, FH = F, MINH = MIN) -> home

Inspect the complete rename() calls for the road and home NBA data sets, including shared identifiers and side-specific score variables. section 6.3.2

How Statistics Slam Dunk covers it

How deep it goes
Explains the left-new/right-old naming rule and demonstrates a complete multi-column rename before deferring to joining and selection.
Examples
Uses NBA road and home columns such as TEAM, VENUE, Q1, and F to create teamR, venueR, Q1R, FR, teamH, venueH, Q1H, and FH.
What it assumes
Assumes basic R data-frame operations and assignment while demonstrating the naming convention from zero.
What you can do after
Apply it via dplyr to rename one or many columns while preserving data for later joins and analysis.
How it connects
Connects rename() with dplyr, filtering, select(), left joins, and factor variables in a data-wrangling pipeline.

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.