liveBook · Manning

Data wrangling

As explained in Statistics Slam Dunk

Data wrangling is the process of cleaning, transforming, reshaping, filtering, summarizing, and combining data for analysis.

Data wrangling prepares imperfect, raw data for accurate analysis. It turns a dataset into a clean, tidy, purpose-built object by reshaping it, filtering rows, selecting columns, changing variable types or names, creating derived variables, summarizing groups, and combining datasets. Because acquired data is rarely ready to analyze, wrangling is a central part of practical data work rather than a preliminary chore. section 2.3 section 2.7

Start with the analytical question, then keep only records and variables that matter. Inspect dimensions and contents after importing; remove irrelevant width and length, derive the fields the question requires, and reshape or join sources until the result supports exploration, visualization, testing, or modeling. Different questions can require different wrangling even when they begin with the same files. section 13.2 section 14.2 section 7.3

Typical operations include:

  • Filter observations by logical conditions and select or deselect variables. section 6.0
  • Reshape between wide and long layouts, then sort or organize data for the intended display. section 2.3 section 6.0
  • Rename variables, convert classes, and create variables derived from existing columns. section 2.3 section 11.4.1
  • Group and summarize data, or join datasets horizontally or vertically into one analysis object. section 1.3 section 11.4.1

Preparing home and road foul data

To compare fouls, split box-score records into road and home teams with filter(), retain needed fields with select(), rename each side to prevent collisions, convert categorical fields to factors, then bind matching rows with cbind(). The result has one row per game with paired home and road measures ready for comparison. section 7.3

Building team salary measures

When franchise histories leave missing seasons, prepare separate subsets for affected teams, select available salary and win columns, calculate total salaries, total wins, efficiency, and mean wins with mutate(), then combine the completed results with rbind(). section 11.4.1

Figure 1.5 A typical chapter flow and, not coincidentally, the typical end-to-end flow of most real-world data science and statistics projects
CH01_F05_Sutton

Use the project-flow diagram to place wrangling between importing data and exploratory, visual, statistical, or modeling work. section 1.3

How Statistics Slam Dunk covers it

How deep it goes
Demonstrates step-by-step dplyr and tidyr operations, including filtering, reshaping, derived variables, summaries, and joins.
Examples
Uses NBA box scores, payrolls, win shares, player seasons, and play-by-play records as messy source data.
What it assumes
Teaches practical wrangling from imported CSV data while using R packages and functions throughout.
What you can do after
Apply it via dplyr, tidyr, readr, and base R to turn raw CSV files into analysis-ready datasets.
How it connects
Connects data wrangling with exploratory data analysis, ggplot2, statistical testing, modeling, and data standardization.

Also explained in

Compare how 5 books explain data wrangling →

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.