Chapter exercise: Into the Tidyverse: arrange, distinct, select, and mutate
The Fastest Flight to North Carolina
This exercise goes with Chapter 7, Into the Tidyverse: arrange, distinct, select, and mutate. It builds on the Filter and Pipe exercise from Chapter 6. It should take you about one hour. Read this whole section before you start.
Download arrange-select-mutate-exercise.Rmd
Save it in a new folder called arrange-select-mutate-exercise inside your geog215 folder, open it in RStudio, and work through it from top to bottom. Everything on this page is also at the top of the file.
Purpose
Skills you’ll practice. By the end of this exercise you will be able to:
- Find the unique values, and unique combinations of values, in a data frame with
distinct(). - Sort rows with
arrange(), smallest first or largest first withdesc(), and by two columns. - Keep, drop, and reorder columns with
select()and the selection helpersstarts_with(),ends_with(), andcontains(). - Make new columns from existing ones with
mutate(), and check that they make sense. - Chain
filter(),mutate(),arrange(), andselect()to answer a question one step at a time.
Knowledge you’ll gain. You’ll find out which airlines and routes connected New York with North Carolina in 2013, which flight to North Carolina left latest, and which was the fastest.
Why this matters. Real data sets have more columns than you need and rarely have the one you want. Census tables come as counts, and you’ll make your own percentages and densities with mutate(). You’ll sort counties to find the highest and lowest, and use select() to keep a table small enough to read. These four verbs, with filter(), are most of the day-to-day work of getting data into shape for a map.
Task
Work through Parts 1 to 7 in order, top to bottom. In each part:
- Read the short explanation, then do each step marked with a bullet (•).
- Wherever you see
___, replace it with the missing code before you run the chunk. A chunk with___in it won’t run. - Before you run each chunk, predict what it will return. Then run it and check.
- Answer each question (Q1, Q2, …) in a sentence or two of Markdown, where it says Your answer here.
- Use the pipe,
|>, for every chain, with one step per line.
| Part | What you’ll do | About |
|---|---|---|
| 1 | Set up your file and keep the flights to North Carolina | 5 min |
| 2 | Find unique values with distinct() |
10 min |
| 3 | Sort rows with arrange() |
10 min |
| 4 | Pick columns with select() |
8 min |
| 5 | Make new columns with mutate() |
15 min |
| 6 | Put the verbs together | 8 min |
| 7 | Reflect, knit, and turn in | 4 min |
Turn in your .Rmd file and your knitted .html file on Canvas.
Criteria for success
Use this checklist before you turn your work in. Each item matches a row of the rubric below.
| Criterion | Full credit | Partial credit | No credit |
|---|---|---|---|
| Knits (2 pts) | Knits to HTML with no errors | Knits, but some chunks have errors shown | Doesn’t knit, or no HTML turned in |
| Code (6 pts) | Every blank filled in correctly; every chunk returns what was asked | Most blanks correct | Many blanks missing or wrong |
| Questions (8 pts) | Q1 to Q11 answered correctly, using the output, with units and reasons | Some answers missing, wrong, or with no units | Most answers missing |
| Part 6 chain (2 pts) | The chain answers the question, in a sensible order of verbs | Runs, but answers a different question | Not done |
| Style (1 pt) | Pipe for every chain, one step per line, snake_case names, no install.packages() |
Several problems | |
| Reflection (1 pt) | All three answered | Missing |
What a good answer looks like. Here is a question, a weak answer, and a strong one.
Question: What was the longest flight from New York in 2013?
Weak answer: “4983.”
Strong answer: “The longest was Hawaiian Airlines’ flight from JFK to Honolulu (HNL), 4,983 miles. I found it by sorting
flightswitharrange(desc(distance)).”
The strong answer names the flight, gives the number with its units, and shows where the number came from.
IMPORTANT! Save often. Before you knit, choose Run > Restart R and Run All Chunks to make sure every chunk works from a fresh start.
|>, which passes the result on its left as
the first argument of the function on its right. Read it as “and then.”
The integrated development environment (IDE) where you write and run R.
R does the computing; RStudio is the workspace around it.
A value counted once, however many times it repeats.
distinct() keeps one row for each unique value, or each
unique combination of values.
R’s table: rows are observations and columns are variables. A tibble is
the tidyverse’s version of a data frame.
A function that picks columns by name inside select(), such
as starts_with(), ends_with(), and
contains().
A block of R code inside an R Markdown file, starting with
```{r} and ending with three backticks.
A simple way of formatting plain text: # for headings,
*italic*, **bold**, and - for
list items.
To build an R Markdown file into a finished document (HTML, PDF, or
Word). Knitting reruns every code chunk from the top in a fresh R
session.
A way of naming variables with lowercase words joined by underscores, as
in pop_chapel_hill. The tidyverse style guide uses it.