2022-05-14 04:46:49 +08:00
# Data transformation {#sec-data-transform}
2015-12-12 02:34:20 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| echo: false
2023-03-02 13:26:43 +08:00
2022-05-14 04:46:49 +08:00
source("_common.R")
2021-05-04 21:10:39 +08:00
```
2016-07-14 23:46:37 +08:00
## Introduction
2023-03-07 21:52:43 +08:00
Visualization is an important tool for generating insight, but it's rare that you get the data in exactly the right form you need to make the graph you want.
2023-03-03 05:09:49 +08:00
Often you'll need to create some new variables or summaries to answer your questions with your data, or maybe you just want to rename the variables or reorder the observations to make the data a little easier to work with.
2023-08-03 03:50:19 +08:00
You'll learn how to do all that (and more!) in this chapter, which will introduce you to data transformation using the **dplyr** package and a new dataset on flights that departed from New York City in 2013.
2022-02-15 00:00:53 +08:00
The goal of this chapter is to give you an overview of all the key tools for transforming a data frame.
2023-03-02 03:34:26 +08:00
We'll start with functions that operate on rows and then columns of a data frame, then circle back to talk more about the pipe, an important tool that you use to combine verbs.
2022-12-16 14:41:10 +08:00
We will then introduce the ability to work with groups.
2023-04-10 23:22:08 +08:00
We will end the chapter with a case study that showcases these functions in action and we'll come back to the functions in more detail in later chapters, as we start to dig into specific types of data (e.g., numbers, strings, dates).
2015-12-09 00:28:54 +08:00
2016-07-08 23:31:52 +08:00
### Prerequisites
2015-12-09 00:28:54 +08:00
2022-02-16 01:59:19 +08:00
In this chapter we'll focus on the dplyr package, another core member of the tidyverse.
2021-02-21 23:40:40 +08:00
We'll illustrate the key ideas using data from the nycflights13 package, and use ggplot2 to help us understand the data.
2015-12-09 00:28:54 +08:00
2022-05-08 13:32:25 +08:00
```{r}
#| label: setup
2015-12-09 00:28:54 +08:00
library(nycflights13)
2016-10-04 01:30:24 +08:00
library(tidyverse)
2015-12-14 23:45:41 +08:00
```
2021-02-21 23:40:40 +08:00
Take careful note of the conflicts message that's printed when you load the tidyverse.
It tells you that dplyr overwrites some functions in base R.
If you want to use the base version of these functions after loading dplyr, you'll need to use their full names: `stats::filter()` and `stats::lag()`.
2022-08-09 02:57:08 +08:00
So far we've mostly ignored which package a function comes from because most of the time it doesn't matter.
2023-05-02 08:24:08 +08:00
However, knowing the package can help you find help and find related functions, so when we need to be precise about which package a function comes from, we'll use the same syntax as R: `packagename::functionname()`.
2016-07-22 22:15:55 +08:00
2016-07-14 23:46:37 +08:00
### nycflights13
2015-12-14 23:45:41 +08:00
2022-02-23 07:48:09 +08:00
To explore the basic dplyr verbs, we're going to use `nycflights13::flights`.
This dataset contains all `r format(nrow(nycflights13::flights), big.mark = ",")` flights that departed from New York City in 2013.
2023-10-30 20:55:02 +08:00
The data comes from the US [Bureau of Transportation Statistics](https://www.transtats.bts.gov/DL_SelectFields.aspx?gnoyr_VQ=FGJ&QO_fu146_anzr=b0-gvzr), and is documented in `?flights`.
2015-12-14 23:45:41 +08:00
```{r}
2016-07-08 23:31:52 +08:00
flights
2015-12-14 23:45:41 +08:00
```
2023-03-03 05:09:49 +08:00
`flights` is a tibble, a special type of data frame used by the tidyverse to avoid some common gotchas.
The most important difference between tibbles and data frames is the way tibbles print; they are designed for large datasets, so they only show the first few rows and only the columns that fit on one screen.
2022-11-19 05:25:52 +08:00
There are a few options to see everything.
If you're using RStudio, the most convenient is probably `View(flights)`, which will open an interactive scrollable and filterable view.
2023-03-03 05:09:49 +08:00
Otherwise you can use `print(flights, width = Inf)` to show all columns, or use `glimpse()`:
2015-12-14 23:45:41 +08:00
2022-11-19 05:25:52 +08:00
```{r}
glimpse(flights)
```
In both views, the variables names are followed by abbreviations that tell you the type of each variable: `<int>` is short for integer, `<dbl>` is short for double (aka real numbers), `<chr>` for character (aka strings), and `<dttm>` for date-time.
2023-04-10 11:21:13 +08:00
These are important because the operations you can perform on a column depend so much on its "type".
2016-10-04 05:08:44 +08:00
2016-10-04 20:49:10 +08:00
### dplyr basics
2015-12-14 23:45:41 +08:00
2023-03-03 05:09:49 +08:00
You're about to learn the primary dplyr verbs (functions) which will allow you to solve the vast majority of your data manipulation challenges.
2022-02-23 07:48:09 +08:00
But before we discuss their individual differences, it's worth stating what they have in common:
2015-12-14 23:45:41 +08:00
2022-02-23 07:48:09 +08:00
1. The first argument is always a data frame.
2015-12-14 23:45:41 +08:00
2023-03-03 05:09:49 +08:00
2. The subsequent arguments typically describe which columns to operate on, using the variable names (without quotes).
2021-02-21 23:40:40 +08:00
2023-03-07 21:52:43 +08:00
3. The output is always a new data frame.
2015-12-14 23:45:41 +08:00
2023-03-03 05:09:49 +08:00
Because each verb does one thing well, solving complex problems will usually require combining multiple verbs, and we'll do so with the pipe, `|>`.
2023-05-02 08:24:08 +08:00
We'll discuss the pipe more in @sec-the-pipe, but in brief, the pipe takes the thing on its left and passes it along to the function on its right so that `x |> f(y)` is equivalent to `f(x, y)`, and `x |> f(y) |> g(z)` is equivalent to `g(f(x, y), z)`.
2022-02-23 07:48:09 +08:00
The easiest way to pronounce the pipe is "then".
2022-09-01 19:43:57 +08:00
That makes it possible to get a sense of the following code even though you haven't yet learned the details:
2021-12-01 22:34:16 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2022-02-23 07:48:09 +08:00
flights |>
filter(dest == "IAH") |>
group_by(year, month, day) |>
summarize(
arr_delay = mean(arr_delay, na.rm = TRUE)
2022-02-15 00:00:53 +08:00
)
```
2021-12-01 22:34:16 +08:00
2023-02-28 21:35:57 +08:00
dplyr's verbs are organized into four groups based on what they operate on: **rows**, **columns**, **groups**, or **tables**.
2023-03-02 03:34:26 +08:00
In the following sections you'll learn the most important verbs for rows, columns, and groups, then we'll come back to the join verbs that work on tables in @sec-joins.
2022-02-15 00:00:53 +08:00
Let's dive in!
2015-12-14 23:45:41 +08:00
2021-04-20 20:59:47 +08:00
## Rows
2023-03-03 05:09:49 +08:00
The most important verbs that operate on rows of a dataset are `filter()`, which changes which rows are present without changing their order, and `arrange()`, which changes the order of the rows without changing which are present.
2022-02-23 07:48:09 +08:00
Both functions only affect the rows, and the columns are left unchanged.
2022-11-21 22:42:55 +08:00
We'll also discuss `distinct()` which finds rows with unique values but unlike `arrange()` and `filter()` it can also optionally modify the columns.
2021-04-21 21:25:39 +08:00
2021-04-20 20:59:47 +08:00
### `filter()`
2015-12-09 00:28:54 +08:00
2022-02-23 07:48:09 +08:00
`filter()` allows you to keep rows based on the values of the columns[^data-transform-1].
2022-02-15 00:00:53 +08:00
The first argument is the data frame.
The second and subsequent arguments are the conditions that must be true to keep the row.
2023-04-10 11:21:13 +08:00
For example, we could find all flights that departed more than 120 minutes (two hours) late:
2015-12-09 00:28:54 +08:00
2022-09-01 19:43:57 +08:00
[^data-transform-1]: Later, you'll learn about the `slice_*()` family which allows you to choose rows based on their positions.
2021-04-21 21:25:39 +08:00
2015-12-09 00:28:54 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
2023-02-28 10:54:34 +08:00
filter(dep_delay > 120)
2015-12-09 00:28:54 +08:00
```
2015-09-21 21:41:14 +08:00
2022-02-23 07:48:09 +08:00
As well as `>` (greater than), you can use `>=` (greater than or equal to), `<` (less than), `<=` (less than or equal to), `==` (equal to), and `!=` (not equal to).
2023-03-07 21:52:43 +08:00
You can also combine conditions with `&` or `,` to indicate "and" (check for both conditions) or with `|` to indicate "or" (check for either condition):
2016-01-01 00:24:18 +08:00
```{r}
2022-02-15 00:00:53 +08:00
# Flights that departed on January 1
flights |>
filter(month == 1 & day == 1)
# Flights that departed in January or February
flights |>
filter(month == 1 | month == 2)
2016-01-01 00:24:18 +08:00
```
2022-02-15 00:00:53 +08:00
There's a useful shortcut when you're combining `|` and `==`: `%in%`.
2022-02-23 07:48:09 +08:00
It keeps rows where the variable equals one of the values on the right:
2016-01-01 00:24:18 +08:00
2022-02-15 00:00:53 +08:00
```{r}
2022-02-23 07:48:09 +08:00
# A shorter way to select flights that departed in January or February
2022-02-15 00:00:53 +08:00
flights |>
filter(month %in% c(1, 2))
```
2015-12-14 23:45:41 +08:00
2022-09-29 23:49:03 +08:00
We'll come back to these comparisons and logical operators in more detail in @sec-logicals.
2022-02-15 00:00:53 +08:00
When you run `filter()` dplyr executes the filtering operation, creating a new data frame, and then prints it.
It doesn't modify the existing `flights` dataset because dplyr functions never modify their inputs.
To save the result, you need to use the assignment operator, `<-`:
```{r}
jan1 <- flights |>
filter(month == 1 & day == 1)
2015-12-15 23:26:47 +08:00
```
2015-12-14 23:45:41 +08:00
2022-02-23 07:48:09 +08:00
### Common mistakes
When you're starting out with R, the easiest mistake to make is to use `=` instead of `==` when testing for equality.
`filter()` will let you know when this happens:
2022-05-14 04:46:49 +08:00
```{r}
#| error: true
2022-02-23 07:48:09 +08:00
flights |>
filter(month = 1)
```
Another mistakes is you write "or" statements like you would in English:
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2022-02-23 07:48:09 +08:00
flights |>
filter(month == 1 | 2)
```
2023-03-03 05:09:49 +08:00
This "works", in the sense that it doesn't throw an error, but it doesn't do what you want because `|` first checks the condition `month == 1` and then checks the condition `2`, which is not a sensible condition to check.
We'll learn more about what's happening here and why in @sec-boolean-operations.
2022-02-23 07:48:09 +08:00
2021-04-20 20:59:47 +08:00
### `arrange()`
2022-02-15 00:00:53 +08:00
`arrange()` changes the order of the rows based on the value of the columns.
2022-02-23 07:48:09 +08:00
It takes a data frame and a set of column names (or more complicated expressions) to order by.
2022-02-15 00:00:53 +08:00
If you provide more than one column name, each additional column will be used to break ties in the values of preceding columns.
For example, the following code sorts by the departure time, which is spread over four columns.
2023-03-03 05:09:49 +08:00
We get the earliest years first, then within a year the earliest months, etc.
2021-04-20 20:59:47 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
arrange(year, month, day, dep_time)
2021-04-20 20:59:47 +08:00
```
2023-03-07 21:52:43 +08:00
You can use `desc()` on a column inside of `arrange()` to re-order the data frame based on that column in descending (big-to-small) order.
For example, this code orders flights from most to least delayed:
2021-04-20 20:59:47 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
arrange(desc(dep_delay))
2021-04-20 20:59:47 +08:00
```
2023-03-03 05:09:49 +08:00
Note that the number of rows has not changed -- we're only arranging the data, we're not filtering it.
2022-11-21 22:42:55 +08:00
### `distinct()`
`distinct()` finds all the unique rows in a dataset, so in a technical sense, it primarily operates on the rows.
2023-01-17 21:35:34 +08:00
Most of the time, however, you'll want the distinct combination of some variables, so you can also optionally supply column names:
2022-11-21 22:42:55 +08:00
```{r}
2023-03-07 21:52:43 +08:00
# Remove duplicate rows, if any
2022-11-21 22:42:55 +08:00
flights |>
distinct()
2023-03-07 21:52:43 +08:00
# Find all unique origin and destination pairs
2022-11-21 22:42:55 +08:00
flights |>
distinct(origin, dest)
```
2023-03-07 21:52:43 +08:00
Alternatively, if you want to the keep other columns when filtering for unique rows, you can use the `.keep_all = TRUE` option.
```{r}
flights |>
distinct(origin, dest, .keep_all = TRUE)
```
2023-03-09 22:04:22 +08:00
It's not a coincidence that all of these distinct flights are on January 1: `distinct()` will find the first occurrence of a unique row in the dataset and discard the rest.
2023-03-07 21:52:43 +08:00
If you want to find the number of occurrences instead, you're better off swapping `distinct()` for `count()`, and with the `sort = TRUE` argument you can arrange them in descending order of number of occurrences.
You'll learn more about count in @sec-counts.
```{r}
flights |>
count(origin, dest, sort = TRUE)
```
2022-11-21 22:42:55 +08:00
2015-12-14 23:45:41 +08:00
### Exercises
2023-05-10 11:18:18 +08:00
1. In a single pipeline for each condition, find all flights that meet the condition:
2015-12-14 23:45:41 +08:00
2023-03-03 05:09:49 +08:00
- Had an arrival delay of two or more hours
- Flew to Houston (`IAH` or `HOU`)
- Were operated by United, American, or Delta
- Departed in summer (July, August, and September)
- Arrived more than two hours late, but didn't leave late
- Were delayed by at least an hour, but made up over 30 minutes in flight
2016-07-15 00:07:51 +08:00
2021-04-20 20:59:47 +08:00
2. Sort `flights` to find the flights with longest departure delays.
2022-02-15 00:00:53 +08:00
Find the flights that left earliest in the morning.
2023-03-03 05:09:49 +08:00
3. Sort `flights` to find the fastest flights.
(Hint: Try including a math calculation inside of your function.)
2015-09-21 21:41:14 +08:00
2023-01-03 15:06:27 +08:00
4. Was there a flight on every day of 2013?
2022-11-21 22:42:55 +08:00
5. Which flights traveled the farthest distance?
Which traveled the least distance?
2015-12-09 00:28:54 +08:00
2023-01-03 09:11:14 +08:00
6. Does it matter what order you used `filter()` and `arrange()` if you're using both?
2022-02-15 00:00:53 +08:00
Why/why not?
Think about the results and how much work the functions would have to do.
2021-04-20 20:59:47 +08:00
## Columns
2023-04-10 11:21:13 +08:00
There are four important verbs that affect the columns without changing the rows: `mutate()` creates new columns that are derived from the existing columns, `select()` changes which columns are present, `rename()` changes the names of the columns, and `relocate()` changes the positions of the columns.
2021-04-20 20:59:47 +08:00
2022-05-14 04:46:49 +08:00
### `mutate()` {#sec-mutate}
2021-04-20 20:59:47 +08:00
2022-02-15 00:00:53 +08:00
The job of `mutate()` is to add new columns that are calculated from the existing columns.
In the transform chapters, you'll learn a large set of functions that you can use to manipulate different types of variables.
For now, we'll stick with basic algebra, which allows us to compute the `gain`, how much time a delayed flight made up in the air, and the `speed` in miles per hour:
2015-12-09 00:28:54 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
mutate(
gain = dep_delay - arr_delay,
speed = distance / air_time * 60
)
2015-12-09 00:28:54 +08:00
```
2022-05-08 10:17:21 +08:00
By default, `mutate()` adds new columns on the right hand side of your dataset, which makes it difficult to see what's happening here.
2021-04-21 21:25:39 +08:00
We can use the `.before` argument to instead add the variables to the left hand side[^data-transform-2]:
2022-05-08 10:17:21 +08:00
[^data-transform-2]: Remember that in RStudio, the easiest way to see a dataset with many columns is `View()`.
2021-04-21 21:25:39 +08:00
2015-12-09 00:28:54 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
mutate(
gain = dep_delay - arr_delay,
speed = distance / air_time * 60,
.before = 1
)
2015-12-09 00:28:54 +08:00
```
2023-03-03 05:09:49 +08:00
The `.` is a sign that `.before` is an argument to the function, not the name of a third new variable we are creating.
2023-01-05 07:41:55 +08:00
You can also use `.after` to add after a variable, and in both `.before` and `.after` you can use the variable name instead of a position.
2023-02-14 03:29:46 +08:00
For example, we could add the new variables after `day`:
2015-12-15 23:26:47 +08:00
2021-04-20 20:59:47 +08:00
```{r}
2023-03-02 03:34:26 +08:00
#| results: false
2023-03-03 05:09:49 +08:00
2022-02-15 00:00:53 +08:00
flights |>
mutate(
gain = dep_delay - arr_delay,
speed = distance / air_time * 60,
.after = day
)
2021-04-20 20:59:47 +08:00
```
2016-07-14 23:46:37 +08:00
2022-02-23 07:48:09 +08:00
Alternatively, you can control which variables are kept with the `.keep` argument.
2023-03-07 21:52:43 +08:00
A particularly useful argument is `"used"` which specifies that we only keep the columns that were involved or created in the `mutate()` step.
2023-03-02 03:34:26 +08:00
For example, the following output will contain only the variables `dep_delay`, `arr_delay`, `air_time`, `gain`, `hours`, and `gain_per_hour`.
2021-02-21 23:40:40 +08:00
2021-04-20 20:59:47 +08:00
```{r}
2023-03-02 03:34:26 +08:00
#| results: false
2023-03-03 05:09:49 +08:00
2022-02-15 00:00:53 +08:00
flights |>
2023-01-23 21:52:18 +08:00
mutate(
2022-02-15 00:00:53 +08:00
gain = dep_delay - arr_delay,
hours = air_time / 60,
gain_per_hour = gain / hours,
.keep = "used"
)
2021-04-20 20:59:47 +08:00
```
2016-07-14 23:46:37 +08:00
2023-03-03 05:09:49 +08:00
Note that since we haven't assigned the result of the above computation back to `flights`, the new variables `gain,` `hours`, and `gain_per_hour` will only be printed but will not be stored in a data frame.
And if we want them to be available in a data frame for future use, we should think carefully about whether we want the result to be assigned back to `flights`, overwriting the original data frame with many more variables, or to a new object.
Often, the right answer is a new object that is named informatively to indicate its contents, e.g., `delay_gain`, but you might also have good reasons for overwriting `flights`.
2022-05-14 04:46:49 +08:00
### `select()` {#sec-select}
2015-12-09 00:28:54 +08:00
2021-02-21 23:40:40 +08:00
It's not uncommon to get datasets with hundreds or even thousands of variables.
2022-02-23 07:48:09 +08:00
In this situation, the first challenge is often just focusing on the variables you're interested in.
2023-03-10 08:00:58 +08:00
`select()` allows you to rapidly zoom in on a useful subset using operations based on the names of the variables:
2015-12-09 00:28:54 +08:00
2023-03-02 03:34:26 +08:00
- Select columns by name:
2021-04-21 21:25:39 +08:00
2023-03-02 03:34:26 +08:00
```{r}
#| results: false
2023-03-03 05:09:49 +08:00
2023-03-02 03:34:26 +08:00
flights |>
select(year, month, day)
```
2021-04-21 21:25:39 +08:00
2023-03-02 03:34:26 +08:00
- Select all columns between year and day (inclusive):
2022-02-15 00:00:53 +08:00
2023-03-02 03:34:26 +08:00
```{r}
#| results: false
2023-03-03 05:09:49 +08:00
2023-03-02 03:34:26 +08:00
flights |>
select(year:day)
```
- Select all columns except those from year to day (inclusive):
```{r}
#| results: false
2023-03-03 05:09:49 +08:00
2023-03-02 03:34:26 +08:00
flights |>
select(!year:day)
```
2023-11-13 22:02:59 +08:00
Historically this operation was done with `-` instead of `!`, so you're likely to see that in the wild. These two operators serve the same purpose but with subtle differences in behavior. We recommend using `!` because it reads as "not" and combines well with `&` and `|`.
2023-03-07 21:52:43 +08:00
2023-03-02 03:34:26 +08:00
- Select all columns that are characters:
```{r}
#| results: false
2023-03-03 05:09:49 +08:00
2023-03-02 03:34:26 +08:00
flights |>
select(where(is.character))
```
2015-12-09 00:28:54 +08:00
2015-12-15 23:26:47 +08:00
There are a number of helper functions you can use within `select()`:
2015-12-09 00:28:54 +08:00
2021-02-21 23:40:40 +08:00
- `starts_with("abc")`: matches names that begin with "abc".
- `ends_with("xyz")`: matches names that end with "xyz".
- `contains("ijk")`: matches names that contain "ijk".
- `num_range("x", 1:3)`: matches `x1`, `x2` and `x3`.
2015-12-15 23:26:47 +08:00
See `?select` for more details.
2023-01-05 10:22:56 +08:00
Once you know regular expressions (the topic of @sec-regular-expressions) you'll also be able to use `matches()` to select variables that match a pattern.
2015-12-15 23:26:47 +08:00
2021-04-20 20:59:47 +08:00
You can rename variables as you `select()` them by using `=`.
The new name appears on the left hand side of the `=`, and the old variable appears on the right hand side:
2015-12-06 22:02:29 +08:00
2015-12-09 00:28:54 +08:00
```{r}
2022-02-23 07:48:09 +08:00
flights |>
select(tail_num = tailnum)
2015-12-09 00:28:54 +08:00
```
2015-09-21 21:41:14 +08:00
2021-04-20 20:59:47 +08:00
### `rename()`
2023-03-03 05:09:49 +08:00
If you want to keep all the existing variables and just want to rename a few, you can use `rename()` instead of `select()`:
2016-07-14 23:46:37 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
rename(tail_num = tailnum)
2016-07-14 23:46:37 +08:00
```
2022-05-11 06:26:04 +08:00
If you have a bunch of inconsistently named columns and it would be painful to fix them all by hand, check out `janitor::clean_names()` which provides some useful automated cleaning.
2021-04-20 20:59:47 +08:00
### `relocate()`
2015-12-15 23:26:47 +08:00
2022-08-29 18:24:32 +08:00
Use `relocate()` to move variables around.
You might want to collect related variables together or move important variables to the front.
By default `relocate()` moves variables to the front:
2015-12-06 22:02:29 +08:00
2015-12-09 00:28:54 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
relocate(time_hour, air_time)
2015-12-09 00:28:54 +08:00
```
2015-09-21 21:41:14 +08:00
2023-03-03 05:09:49 +08:00
You can also specify where to put them using the `.before` and `.after` arguments, just like in `mutate()`:
2015-12-09 00:28:54 +08:00
```{r}
2023-03-02 03:34:26 +08:00
#| results: false
2022-02-15 00:00:53 +08:00
flights |>
relocate(year:dep_time, .after = time_hour)
flights |>
relocate(starts_with("arr"), .before = dep_time)
2015-12-09 00:28:54 +08:00
```
2015-12-15 23:26:47 +08:00
### Exercises
2015-12-09 00:28:54 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
#| echo: false
2021-03-29 21:58:27 +08:00
# For data checking, not used in results shown in book
2022-02-24 03:15:52 +08:00
flights <- flights |> mutate(
2015-12-15 23:26:47 +08:00
dep_time = hour * 60 + minute,
arr_time = (arr_time %/% 100) * 60 + (arr_time %% 100),
airtime2 = arr_time - dep_time,
dep_sched = dep_time + dep_delay
2015-12-14 23:45:41 +08:00
)
2015-12-16 23:58:52 +08:00
2022-12-05 16:12:12 +08:00
ggplot(flights, aes(x = dep_sched)) + geom_histogram(binwidth = 60)
ggplot(flights, aes(x = dep_sched %% 60)) + geom_histogram(binwidth = 1)
ggplot(flights, aes(x = air_time - airtime2)) + geom_histogram()
2015-12-14 23:45:41 +08:00
```
2015-12-09 00:28:54 +08:00
2023-02-08 00:19:55 +08:00
1. Compare `dep_time`, `sched_dep_time`, and `dep_delay`.
2021-02-21 23:40:40 +08:00
How would you expect those three numbers to be related?
2015-12-29 23:59:14 +08:00
2023-02-08 00:19:55 +08:00
2. Brainstorm as many ways as possible to select `dep_time`, `dep_delay`, `arr_time`, and `arr_delay` from `flights`.
2021-04-20 20:59:47 +08:00
2023-03-07 21:52:43 +08:00
3. What happens if you specify the name of the same variable multiple times in a `select()` call?
2021-04-20 20:59:47 +08:00
2023-02-08 00:19:55 +08:00
4. What does the `any_of()` function do?
2021-04-20 20:59:47 +08:00
Why might it be helpful in conjunction with this vector?
```{r}
variables <- c("year", "month", "day", "dep_delay", "arr_delay")
```
2023-02-08 00:19:55 +08:00
5. Does the result of running the following code surprise you?
2023-03-03 05:09:49 +08:00
How do the select helpers deal with upper and lower case by default?
2021-04-20 20:59:47 +08:00
How can you change that default?
2016-07-22 22:15:55 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2023-03-03 05:09:49 +08:00
flights |> select(contains("TIME"))
2021-04-20 20:59:47 +08:00
```
2021-02-21 23:40:40 +08:00
2023-02-28 10:54:34 +08:00
6. Rename `air_time` to `air_time_min` to indicate units of measurement and move it to the beginning of the data frame.
2023-03-03 05:09:49 +08:00
7. Why doesn't the following work, and what does the error mean?
```{r}
#| error: true
flights |>
select(tailnum) |>
arrange(arr_delay)
```
2023-03-10 08:00:58 +08:00
## The pipe {#sec-the-pipe}
2023-03-02 03:34:26 +08:00
We've shown you simple examples of the pipe above, but its real power arises when you start to combine multiple verbs.
2024-02-27 04:27:18 +08:00
For example, imagine that you wanted to find the fastest flights to Houston's IAH airport: you need to combine `filter()`, `mutate()`, `select()`, and `arrange()`:
2023-03-02 03:34:26 +08:00
```{r}
flights |>
filter(dest == "IAH") |>
mutate(speed = distance / air_time * 60) |>
select(year:day, dep_time, carrier, flight, speed) |>
arrange(desc(speed))
```
2023-04-10 11:21:13 +08:00
Even though this pipeline has four steps, it's easy to skim because the verbs come at the start of each line: start with the `flights` data, then filter, then mutate, then select, then arrange.
2023-03-02 03:34:26 +08:00
What would happen if we didn't have the pipe?
We could nest each function call inside the previous call:
```{r}
#| results: false
arrange(
select(
mutate(
filter(
flights,
dest == "IAH"
),
speed = distance / air_time * 60
),
year:day, dep_time, carrier, flight, speed
),
desc(speed)
)
```
2023-03-03 05:09:49 +08:00
Or we could use a bunch of intermediate objects:
2023-03-02 03:34:26 +08:00
```{r}
#| results: false
flights1 <- filter(flights, dest == "IAH")
flights2 <- mutate(flights1, speed = distance / air_time * 60)
flights3 <- select(flights2, year:day, dep_time, carrier, flight, speed)
arrange(flights3, desc(speed))
```
While both forms have their time and place, the pipe generally produces data analysis code that is easier to write and read.
2023-10-26 10:11:30 +08:00
To add the pipe to your code, we recommend using the built-in keyboard shortcut Ctrl/Cmd + Shift + M.
2023-03-02 03:34:26 +08:00
You'll need to make one change to your RStudio options to use `|>` instead of `%>%` as shown in @fig-pipe-options; more on `%>%` shortly.
```{r}
#| label: fig-pipe-options
#| echo: false
2023-05-26 23:44:08 +08:00
#| fig-cap: |
2023-03-02 03:34:26 +08:00
#| To insert `|>`, make sure the "Use native pipe operator" option is checked.
2023-05-26 23:44:08 +08:00
#| fig-alt: |
2023-03-02 03:34:26 +08:00
#| Screenshot showing the "Use native pipe operator" option which can
#| be found on the "Editing" panel of the "Code" options.
knitr::include_graphics("screenshots/rstudio-pipe-options.png")
```
::: callout-note
## magrittr
If you've been using the tidyverse for a while, you might be familiar with the `%>%` pipe provided by the **magrittr** package.
The magrittr package is included in the core tidyverse, so you can use `%>%` whenever you load the tidyverse:
```{r}
#| eval: false
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
summarize(n = n())
```
For simple cases, `|>` and `%>%` behave identically.
So why do we recommend the base pipe?
Firstly, because it's part of base R, it's always available for you to use, even when you're not using the tidyverse.
Secondly, `|>` is quite a bit simpler than `%>%`: in the time between the invention of `%>%` in 2014 and the inclusion of `|>` in R 4.1.0 in 2021, we gained a better understanding of the pipe.
This allowed the base implementation to jettison infrequently used and less important features.
:::
2021-04-20 20:59:47 +08:00
## Groups
2016-07-22 22:15:55 +08:00
2022-02-16 01:59:19 +08:00
So far you've learned about functions that work with rows and columns.
dplyr gets even more powerful when you add in the ability to work with groups.
2022-05-08 13:32:25 +08:00
In this section, we'll focus on the most important functions: `group_by()`, `summarize()`, and the slice family of functions.
2021-04-21 21:25:39 +08:00
2021-04-20 20:59:47 +08:00
### `group_by()`
2015-12-09 00:28:54 +08:00
2021-04-21 21:25:39 +08:00
Use `group_by()` to divide your dataset into groups meaningful for your analysis:
2015-12-09 00:28:54 +08:00
2015-12-15 23:26:47 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
group_by(month)
2015-12-15 23:26:47 +08:00
```
2015-12-09 00:28:54 +08:00
2023-03-07 21:52:43 +08:00
`group_by()` doesn't change the data but, if you look closely at the output, you'll notice that the output indicates that it is "grouped by" month (`Groups: month [12]`).
2022-02-16 01:59:19 +08:00
This means subsequent operations will now work "by month".
2023-03-07 21:52:43 +08:00
`group_by()` adds this grouped feature (referred to as class) to the data frame, which changes the behavior of the subsequent verbs applied to the data.
2016-07-14 23:46:37 +08:00
2022-05-14 04:46:49 +08:00
### `summarize()` {#sec-summarize}
2021-04-20 20:59:47 +08:00
2023-03-03 05:09:49 +08:00
The most important grouped operation is a summary, which, if being used to calculate a single summary statistic, reduces the data frame to have a single row for each group.
2023-04-10 11:21:13 +08:00
In dplyr, this operation is performed by `summarize()`[^data-transform-3], as shown by the following example, which computes the average departure delay by month:
2021-04-21 21:25:39 +08:00
2022-11-19 06:07:54 +08:00
[^data-transform-3]: Or `summarise()`, if you prefer British English.
2015-12-09 00:28:54 +08:00
2015-12-15 23:26:47 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
group_by(month) |>
2022-05-08 13:32:25 +08:00
summarize(
2023-03-03 05:09:49 +08:00
avg_delay = mean(dep_delay)
2022-02-15 00:00:53 +08:00
)
2021-04-20 20:59:47 +08:00
```
2015-12-16 23:58:52 +08:00
2022-02-15 00:00:53 +08:00
Uhoh!
2023-03-03 05:09:49 +08:00
Something has gone wrong and all of our results are `NA`s (pronounced "N-A"), R's symbol for missing value.
This happened because some of the observed flights had missing data in the delay column, and so when we calculated the mean including those values, we got an `NA` result.
We'll come back to discuss missing values in detail in @sec-missing-values, but for now we'll tell the `mean()` function to ignore all missing values by setting the argument `na.rm` to `TRUE`:
2015-12-15 23:26:47 +08:00
2021-04-20 20:59:47 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
group_by(month) |>
2022-05-08 13:32:25 +08:00
summarize(
2023-11-30 14:05:30 +08:00
avg_delay = mean(dep_delay, na.rm = TRUE)
2022-02-15 00:00:53 +08:00
)
2021-04-20 20:59:47 +08:00
```
2015-12-16 23:58:52 +08:00
2022-05-08 13:32:25 +08:00
You can create any number of summaries in a single call to `summarize()`.
2022-02-15 00:00:53 +08:00
You'll learn various useful summaries in the upcoming chapters, but one very useful summary is `n()`, which returns the number of rows in each group:
2015-12-09 00:28:54 +08:00
```{r}
2022-02-15 00:00:53 +08:00
flights |>
group_by(month) |>
2022-05-08 13:32:25 +08:00
summarize(
2023-11-30 14:05:30 +08:00
avg_delay = mean(dep_delay, na.rm = TRUE),
2022-02-15 00:00:53 +08:00
n = n()
)
2015-12-09 00:28:54 +08:00
```
2022-02-15 00:00:53 +08:00
Means and counts can get you a surprisingly long way in data science!
2015-12-15 23:26:47 +08:00
2022-02-16 01:59:19 +08:00
### The `slice_` functions
2023-03-03 05:09:49 +08:00
There are five handy functions that allow you extract specific rows within each group:
2022-02-16 01:59:19 +08:00
- `df |> slice_head(n = 1)` takes the first row from each group.
- `df |> slice_tail(n = 1)` takes the last row in each group.
2023-03-03 05:09:49 +08:00
- `df |> slice_min(x, n = 1)` takes the row with the smallest value of column `x`.
- `df |> slice_max(x, n = 1)` takes the row with the largest value of column `x`.
2022-11-19 05:27:05 +08:00
- `df |> slice_sample(n = 1)` takes one random row.
2022-02-16 01:59:19 +08:00
2022-02-23 07:48:09 +08:00
You can vary `n` to select more than one row, or instead of `n =`, you can use `prop = 0.1` to select (e.g.) 10% of the rows in each group.
2023-03-03 05:09:49 +08:00
For example, the following code finds the flights that are most delayed upon arrival at each destination:
2022-02-16 01:59:19 +08:00
```{r}
flights |>
group_by(dest) |>
2023-03-03 05:09:49 +08:00
slice_max(arr_delay, n = 1) |>
relocate(dest)
2022-02-16 01:59:19 +08:00
```
2023-03-09 07:41:23 +08:00
Note that there are 105 destinations but we get 108 rows here.
What's up?
2023-03-26 22:08:12 +08:00
`slice_min()` and `slice_max()` keep tied values so `n = 1` means give us all rows with the highest value.
If you want exactly one row per group you can set `with_ties = FALSE`.
2023-03-09 07:41:23 +08:00
This is similar to computing the max delay with `summarize()`, but you get the whole corresponding row (or rows if there's a tie) instead of the single summary statistic.
2022-02-16 01:59:19 +08:00
2021-04-20 20:59:47 +08:00
### Grouping by multiple variables
2021-03-29 21:58:27 +08:00
2022-02-23 07:48:09 +08:00
You can create groups using more than one variable.
2023-03-07 21:52:43 +08:00
For example, we could make a group for each date.
2021-03-29 21:58:27 +08:00
```{r}
2022-02-16 01:59:19 +08:00
daily <- flights |>
2022-02-15 00:00:53 +08:00
group_by(year, month, day)
2021-03-29 21:58:27 +08:00
daily
```
2022-02-16 01:59:19 +08:00
When you summarize a tibble grouped by more than one variable, each summary peels off the last group.
2023-02-28 21:35:57 +08:00
In hindsight, this wasn't a great way to make this function work, but it's difficult to change without breaking existing code.
2022-02-16 01:59:19 +08:00
To make it obvious what's happening, dplyr displays a message that tells you how you can change this behavior:
2021-03-29 21:58:27 +08:00
```{r}
2022-02-24 03:15:52 +08:00
daily_flights <- daily |>
2023-03-03 05:09:49 +08:00
summarize(n = n())
2021-03-29 21:58:27 +08:00
```
2022-02-23 07:48:09 +08:00
If you're happy with this behavior, you can explicitly request it in order to suppress the message:
2021-03-29 21:58:27 +08:00
2022-05-08 13:32:25 +08:00
```{r}
#| results: false
2022-02-24 03:15:52 +08:00
daily_flights <- daily |>
2022-05-08 13:32:25 +08:00
summarize(
2022-02-15 00:00:53 +08:00
n = n(),
.groups = "drop_last"
)
2021-03-29 21:58:27 +08:00
```
2023-04-10 23:22:08 +08:00
Alternatively, change the default behavior by setting a different value, e.g., `"drop"` to drop all grouping or `"keep"` to preserve the same groups.
2021-03-29 21:58:27 +08:00
### Ungrouping
2023-03-03 05:09:49 +08:00
You might also want to remove grouping from a data frame without using `summarize()`.
2022-02-16 01:59:19 +08:00
You can do this with `ungroup()`.
2021-03-29 21:58:27 +08:00
2023-03-03 05:09:49 +08:00
```{r}
daily |>
ungroup()
```
Now let's see what happens when you summarize an ungrouped data frame.
2021-03-29 21:58:27 +08:00
```{r}
2022-02-24 03:15:52 +08:00
daily |>
ungroup() |>
2022-05-08 13:32:25 +08:00
summarize(
2023-03-03 05:09:49 +08:00
avg_delay = mean(dep_delay, na.rm = TRUE),
2021-04-20 20:59:47 +08:00
flights = n()
)
2021-03-29 21:58:27 +08:00
```
2023-03-03 05:09:49 +08:00
You get a single row back because dplyr treats all the rows in an ungrouped data frame as belonging to one group.
2021-12-01 22:34:16 +08:00
2023-03-10 05:07:18 +08:00
### `.by`
2023-06-13 19:55:56 +08:00
dplyr 1.1.0 includes a new, experimental, syntax for per-operation grouping, the `.by` argument.
2023-03-10 05:07:18 +08:00
`group_by()` and `ungroup()` aren't going away, but you can now also use the `.by` argument to group within a single operation:
```{r}
#| results: false
flights |>
summarize(
delay = mean(dep_delay, na.rm = TRUE),
n = n(),
.by = month
)
```
Or if you want to group by multiple variables:
```{r}
#| results: false
flights |>
summarize(
delay = mean(dep_delay, na.rm = TRUE),
n = n(),
.by = c(origin, dest)
)
```
`.by` works with all verbs and has the advantage that you don't need to use the `.groups` argument to suppress the grouping message or `ungroup()` when you're done.
2023-05-21 12:01:04 +08:00
We didn't focus on this syntax in this chapter because it was very new when we wrote the book.
2023-03-10 05:07:18 +08:00
We did want to mention it because we think it has a lot of promise and it's likely to be quite popular.
You can learn more about it in the [dplyr 1.1.0 blog post](https://www.tidyverse.org/blog/2023/02/dplyr-1-1-0-per-operation-grouping/).
2021-04-20 20:59:47 +08:00
### Exercises
2023-03-03 05:09:49 +08:00
1. Which carrier has the worst average delays?
2021-04-20 20:59:47 +08:00
Challenge: can you disentangle the effects of bad airports vs. bad carriers?
Why/why not?
2022-05-08 13:32:25 +08:00
(Hint: think about `flights |> group_by(carrier, dest) |> summarize(n())`)
2022-02-23 07:48:09 +08:00
2023-03-03 05:09:49 +08:00
2. Find the flights that are most delayed upon departure from each destination.
2022-02-23 07:48:09 +08:00
3. How do delays vary over the course of the day.
Illustrate your answer with a plot.
4. What happens if you supply a negative `n` to `slice_min()` and friends?
2021-04-20 20:59:47 +08:00
2023-02-08 00:41:53 +08:00
5. Explain what `count()` does in terms of the dplyr verbs you just learned.
2022-02-23 07:48:09 +08:00
What does the `sort` argument to `count()` do?
2021-04-20 20:59:47 +08:00
2023-01-03 09:44:46 +08:00
6. Suppose we have the following tiny data frame:
```{r}
df <- tibble(
x = 1:5,
y = c("a", "b", "a", "a", "b"),
z = c("K", "K", "L", "L", "K")
)
```
2023-03-03 05:09:49 +08:00
a. Write down what you think the output will look like, then check if you were correct, and describe what `group_by()` does.
2023-01-03 09:44:46 +08:00
```{r}
#| eval: false
df |>
group_by(y)
```
2023-03-03 05:09:49 +08:00
b. Write down what you think the output will look like, then check if you were correct, and describe what `arrange()` does.
2023-01-03 09:44:46 +08:00
Also comment on how it's different from the `group_by()` in part (a)?
```{r}
#| eval: false
df |>
arrange(y)
```
2023-03-03 05:09:49 +08:00
c. Write down what you think the output will look like, then check if you were correct, and describe what the pipeline does.
2023-01-03 09:44:46 +08:00
```{r}
#| eval: false
df |>
group_by(y) |>
summarize(mean_x = mean(x))
```
2023-03-03 05:09:49 +08:00
d. Write down what you think the output will look like, then check if you were correct, and describe what the pipeline does.
2023-01-03 09:44:46 +08:00
Then, comment on what the message says.
```{r}
#| eval: false
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
```
2023-03-03 05:09:49 +08:00
e. Write down what you think the output will look like, then check if you were correct, and describe what the pipeline does.
2023-01-03 09:44:46 +08:00
How is the output different from the one in part (d).
```{r}
#| eval: false
df |>
group_by(y, z) |>
summarize(mean_x = mean(x), .groups = "drop")
```
2023-03-03 05:09:49 +08:00
f. Write down what you think the outputs will look like, then check if you were correct, and describe what each pipeline does.
2023-01-03 09:44:46 +08:00
How are the outputs of the two pipelines different?
```{r}
#| eval: false
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
df |>
group_by(y, z) |>
mutate(mean_x = mean(x))
```
2022-05-14 04:46:49 +08:00
## Case study: aggregates and sample size {#sec-sample-size}
2015-12-09 00:28:54 +08:00
2022-02-16 01:59:19 +08:00
Whenever you do any aggregation, it's always a good idea to include a count (`n()`).
2022-05-08 10:17:21 +08:00
That way, you can ensure that you're not drawing conclusions based on very small amounts of data.
2023-03-02 13:26:43 +08:00
We'll demonstrate this with some baseball data from the **Lahman** package.
2023-03-03 05:09:49 +08:00
Specifically, we will compare what proportion of times a player gets a hit (`H`) vs. the number of times they try to put the ball in play (`AB`):
2016-08-01 00:32:16 +08:00
2022-02-15 00:00:53 +08:00
```{r}
2022-02-24 03:15:52 +08:00
batters <- Lahman::Batting |>
group_by(playerID) |>
2022-05-08 13:32:25 +08:00
summarize(
2023-03-02 13:26:43 +08:00
performance = sum(H, na.rm = TRUE) / sum(AB, na.rm = TRUE),
2022-02-16 01:59:19 +08:00
n = sum(AB, na.rm = TRUE)
2022-02-15 00:00:53 +08:00
)
batters
```
2023-03-02 13:26:43 +08:00
When we plot the skill of the batter (measured by the batting average, `performance`) against the number of opportunities to hit the ball (measured by times at bat, `n`), you see two patterns:
2015-12-16 23:58:52 +08:00
2023-03-03 05:09:49 +08:00
1. The variation in `performance` is larger among players with fewer at-bats.
2023-03-02 13:26:43 +08:00
The shape of this plot is very characteristic: whenever you plot a mean (or other summary statistics) vs. group size, you'll see that the variation decreases as the sample size increases[^data-transform-4].
2021-02-21 23:40:40 +08:00
2023-03-07 21:52:43 +08:00
2. There's a positive correlation between skill (`performance`) and opportunities to hit the ball (`n`) because teams want to give their best batters the most opportunities to hit the ball.
2023-03-02 13:26:43 +08:00
[^data-transform-4]: \*cough\* the law of large numbers \*cough\*.
2015-12-09 00:28:54 +08:00
2022-05-08 13:32:25 +08:00
```{r}
#| warning: false
2023-05-26 23:44:08 +08:00
#| fig-alt: |
2023-03-02 13:26:43 +08:00
#| A scatterplot of number of batting performance vs. batting opportunites
2022-02-16 01:59:19 +08:00
#| overlaid with a smoothed line. Average performance increases sharply
#| from 0.2 at when n is 1 to 0.25 when n is ~1000. Average performance
#| continues to increase linearly at a much shallower slope reaching
#| ~0.3 when n is ~15,000.
2022-05-08 13:32:25 +08:00
2022-02-24 03:15:52 +08:00
batters |>
filter(n > 100) |>
2023-03-02 13:26:43 +08:00
ggplot(aes(x = n, y = performance)) +
geom_point(alpha = 1 / 10) +
geom_smooth(se = FALSE)
2015-12-09 00:28:54 +08:00
```
2023-03-02 13:26:43 +08:00
Note the handy pattern for combining ggplot2 and dplyr.
2023-03-03 05:09:49 +08:00
You just have to remember to switch from `|>`, for dataset processing, to `+` for adding layers to your plot.
2023-03-02 13:26:43 +08:00
2021-02-21 23:40:40 +08:00
This also has important implications for ranking.
2023-03-07 21:52:43 +08:00
If you naively sort on `desc(performance)`, the people with the best batting averages are clearly the ones who tried to put the ball in play very few times and happened to get a hit, they're not necessarily the most skilled players:
2015-12-16 23:58:52 +08:00
```{r}
2022-02-24 03:15:52 +08:00
batters |>
2023-03-02 13:26:43 +08:00
arrange(desc(performance))
2015-12-09 00:28:54 +08:00
```
2022-09-01 19:43:57 +08:00
You can find a good explanation of this problem and how to overcome it at <http://varianceexplained.org/r/empirical_bayes_baseball/> and <https://www.evanmiller.org/how-not-to-sort-by-average-rating.html>.
2022-09-29 23:36:22 +08:00
## Summary
In this chapter, you've learned the tools that dplyr provides for working with data frames.
2022-11-19 06:07:54 +08:00
The tools are roughly grouped into three categories: those that manipulate the rows (like `filter()` and `arrange()`, those that manipulate the columns (like `select()` and `mutate()`), and those that manipulate groups (like `group_by()` and `summarize()`).
2022-09-29 23:36:22 +08:00
In this chapter, we've focused on these "whole data frame" tools, but you haven't yet learned much about what you can do with the individual variable.
2022-09-29 23:49:03 +08:00
We'll come back to that in the Transform part of the book, where each chapter will give you tools for a specific type of variable.
2022-09-29 23:36:22 +08:00
2023-03-02 03:34:26 +08:00
In the next chapter, we'll pivot back to workflow to discuss the importance of code style, keeping your code well organized in order to make it easy for you and others to read and understand your code.