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
Use the project-flow diagram to place wrangling between importing data and exploratory, visual, statistical, or modeling work. section 1.3




