CMCE10002 Chap.6 Wrangling One Table with dplyr
Wrangling One Table with dplyr
Transformation with a stated purpose
The week 4 lecture defines wrangling as reshaping a messy extract on purpose, until the table is clean, well structured and fit for one specific purpose.
There is no generally clean dataset, only a dataset shaped for the question about to be asked, and the same source can need two different shapes for two different questions.
Six verbs, arranged as three pairs
The verbs divide into row operations, column operations and group operations.
Four of the six leave the row or column count recognisable; only the last changes the grain, and changing the grain is the operation that most often produces a result that is technically correct and answers a different question.
Compute first, then narrow
A derived column has to be built while its inputs are still present, and the rows that would break the calculation have to be removed before it runs rather than cleaned up afterwards.
A rate computed on an impossible denominator arrives as an ordinary-looking number and lands at the top of the ranking.
Four chains cover most of the week
Nearly every task inside a single table is one of four sequences: rank the largest few, build a comparable rate and rank on it, cut a tidy extract for a colleague, or count something specific.
Two orderings inside those chains are load-bearing rather than stylistic. Removing unusable rows precedes ranking, because the top of a sorted table is exactly where a reader looks; building a derived column precedes narrowing the columns, because a column already dropped cannot be used in a calculation.
What this chapter covers
- 01
Wrangling defined as transformation fit for one stated purpose
- 02
Row, column and group operations as three pairs of verbs
- 03
Filtering before ranking, and why the order is not stylistic
- 04
Turning a dollar total into a comparable rate
- 05
Selecting by name, by pattern and by type, and which survives a change in the data
From a dollar total to a comparable rate
- 1Say why a dollar total will not compare across businesses of different sizes.
- 1Compute the rate for each firm.
- 1State the two rankings and how they differ.
- 1Name the guard the calculation needs.
Key terms
- Data wrangling
- Reshaping a messy extract on purpose until the table is fit for one specific purpose.
- Row operation
- A step that keeps a subset of rows or reorders them without changing the columns.
- Column operation
- A step that adds a computed column or keeps a subset of the existing columns.
- Derived measure
- A quantity built from other columns, such as a rate that divides a total by the base it was earned on.
- Return on invested capital
- Operating profit divided by the capital invested in the business, taken at book value.
- Descending order
- A sort that places the largest value first, which is not the default behaviour.
Wrangling One Table with dplyr FAQ
Which comes first, filtering or ranking?
Filtering. Ranking a table that still contains rows with missing or impossible values puts those rows exactly where a reader looks, which is the top of a sorted table. Removing them first is cheaper than explaining them afterwards.
Why can a ranking by profit and a ranking by return disagree completely?
Because a dollar total scales with how big the business is, while a rate divides that total by the capital employed and takes size out of the comparison. One ranking answers who earned the most money; the other answers who worked each dollar of capital hardest.
What happens to the number of columns when rows are filtered?
Nothing. Filtering keeps the rows that satisfy a condition and leaves every column in place. Only a column operation changes how many columns a table has.
Exam move
For every task, say which of the three pairs it belongs to before reaching for a verb. That single question narrows the choice to two candidates that do obviously different things, and it removes most of the syntax hesitation in this part of the subject.
Working through Wrangling One Table with dplyr in CMCE10002? Sia is AskSia’s AI Statistics tutor — ask any CMCE10002 Wrangling One Table with dplyr question and get a clear, step-by-step explanation grounded in how CMCE10002 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.