Chapter exercise: Into the Tidyverse: glimpse, filter, and the Pipe
Flights to North Carolina
This exercise goes with Chapter 6, Into the Tidyverse: glimpse, filter, and the Pipe. It builds on the Indexing exercise from Chapter 4. It should take you about one hour. Read this whole section before you start.
Download filter-pipe-exercise.Rmd
Save it in a new folder called filter-pipe-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:
- Load the tidyverse and a data package, and look inside a data frame with
glimpse(). - Keep the rows you need with
filter(), using one condition or several. - Choose between AND (
,or&) and OR (|), and predict which gives more rows. - Chain steps together with the pipe,
|>, and read a chain top to bottom. - Count rows with
count()andnrow(). - Find missing values with
is.na(), and explain why base R brackets andfilter()can give different counts.
Knowledge you’ll gain. You’ll find out how many flights left New York City for North Carolina’s airports in 2013, which North Carolina airport got the most, and how many of the flights to Raleigh–Durham were cancelled.
Why this matters. Almost every analysis this semester starts with a filter: the counties in one state, the tracts in one city, the years after 2010. filter() and the pipe make those steps short enough to read at a glance, so you and anyone checking your work can see exactly which rows you kept. Missing values are the most common reason two “correct” methods give different answers, and nothing warns you when it happens. Learning to check for them now will save you from wrong numbers in your final project.
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 how many rows 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, as the instructions show.
| Part | What you’ll do | About |
|---|---|---|
| 1 | Set up your file and load the data | 5 min |
| 2 | Look inside flights with glimpse() |
8 min |
| 3 | Filter with one condition | 10 min |
| 4 | Filter with several conditions: AND and OR | 12 min |
| 5 | Chain steps with the pipe | 8 min |
| 6 | Missing values: why the counts don’t match | 12 min |
| 7 | Reflect, knit, and turn in | 5 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 |
| Filter 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 reasons | Some answers missing, wrong, or with no reason | Most answers missing |
| Missing values (2 pts) | Part 6 explains why the two counts differ, and fixes it with is.na() |
Counts given, but not explained | Not done |
| Style (1 pt) | Pipe for every chain, one step per line, == and is.na() used correctly, 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: How many flights left New York for Raleigh–Durham in 2013?
Weak answer: “8163.”
Strong answer: “8,163 flights left New York City’s three airports for Raleigh–Durham (RDU) in 2013. That’s about 22 a day. I counted them with
filter(dest == "RDU")andcount().”
The strong answer gives the number with its units, puts it in context, and says how it was found.
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 bundle of code, written by other users, that adds new functions to R.
Install it once with install.packages(), then load it in
each session with library().
R’s table: rows are observations and columns are variables. A tibble is
the tidyverse’s version of a data frame.
A value that is unknown, written NA (“not available”) in R.
Most calculations with NA give NA; test for it
with is.na().
A small area of about 1,200 to 8,000 people (4,000 on average), drawn to
be fairly stable over time. The workhorse unit for neighborhood
analysis.
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.