The University of Melbourne · FACULTY OF STATISTICS

CMCE10002 Chap.6 Wrangling One Table with dplyr

- one subject, every graph, every model, every mark
5 Chapters5-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 6 of 13 · CMCE10002

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.

In this chapter

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

Worked example · free

From a dollar total to a comparable rate

Q [4 marks]. AskSia assigns four practice points to this independent exercise; they are not a University marking scheme. Firm A reports operating profit of $2,400m on invested capital of $16,000m. Firm B reports $180m on $600m. Rank them, and say which ranking answers which question.
  • 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.
Profit is counted in dollars, so any business turning over a very large volume of activity posts very large gains or losses more or less by construction. Dividing operating profit by invested capital converts size into a rate: Firm A returns 0.15, or fifteen cents per dollar invested, and Firm B returns 0.30. Firm A earns roughly thirteen times the dollar profit and half the return, so ranking by dollars puts A first while ranking by the rate puts B first. The guard is to keep only rows with a positive denominator before the division, because a zero or negative denominator produces values that are wrong in the direction that puts them at the top.
Sia tip — Build the derived column while its inputs are still in the table. A column that has already been dropped cannot be used in a calculation.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 74 of your The University of Melbourne subjects - and 1,000+ Bibles across every Australian university.
Sia - your CMCE10002 tutor, unlimited, worked the way the exam marks it
The full 5-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
Unlock the full CMCE10002 Bible + 74 The University of Melbourne subjects
$0.99 Trial