Skip to main content

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.

TipGet the exercise file

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:

  1. Find the unique values, and unique combinations of values, in a data frame with distinct().
  2. Sort rows with arrange(), smallest first or largest first with desc(), and by two columns.
  3. Keep, drop, and reorder columns with select() and the selection helpers starts_with(), ends_with(), and contains().
  4. Make new columns from existing ones with mutate(), and check that they make sense.
  5. Chain filter(), mutate(), arrange(), and select() 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 flights with arrange(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.