September 9 + 11, 2024
Much of this material can be found at the introduction to dplyr vignette.
The Active Duty data are not tidy! What are the cases? How are the data not tidy? What might the data look like in tidy form? Suppose that the case was “an individual in the armed forces.” What variables would you use to capture the information in the following table?
Hosted online:
Hosted locally:
Things to note:
View()
can be used in RStudio to bring up an excel-style spreadsheet. Only for viewing, not editing!View()
has a capital letter V
.View()
should not be used in the Quarto document.dim()
is used to find the dimensions (rows x columns).names()
is used to find the names of the variables.head()
is used to print the first several lines of the dataset to the console.For now, we’ll work with all flights out of the three NYC airports in 2013.
Whenever you’re learning a new tool, for a long time you’re going to suck … but the good news is that is typical, that’s something that happens to everyone, and it’s only temporary.
-Hadley Wickham
Data sets are often of high volume (lots of rows) and high variety (lots of columns). This is overwhelming to visualize and analyze, so we find ourselves chopping the data set up into more manageable and meaningful chunks. We also often need to perform operations to organize and clean our data.
This is all possible in base R, but with dplyr
, it is simple, readable, and fast.
Most data wrangling happens with a set of data verbs. Verbs are functions that act on data frames.
The first argument of each data verb is the data frame.
filter()
arrange()
select()
distinct()
mutate()
summarize()
sample_n()
filter()
Allows you to select a subset of the rows of a data frame. The first argument is the name of the data frame, the following arguments are the filters that you’d like to apply
For all flights on January 1st:
# A tibble: 842 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
<int> <int> <int> <int> <int> <dbl> <int> <int>
1 2013 1 1 517 515 2 830 819
2 2013 1 1 533 529 4 850 830
3 2013 1 1 542 540 2 923 850
4 2013 1 1 544 545 -1 1004 1022
5 2013 1 1 554 600 -6 812 837
6 2013 1 1 554 558 -4 740 728
7 2013 1 1 555 600 -5 913 854
8 2013 1 1 557 600 -3 709 723
9 2013 1 1 557 600 -3 838 846
10 2013 1 1 558 600 -2 753 745
# ℹ 832 more rows
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>
Filters are constructed of logical operators: <
, >
, <=
, >=
, ==
, !=
(and some others).
Adding them one by one to filter()
is akin to saying “this AND that”. To say “this OR that OR both”, use |.
# A tibble: 51,955 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
<int> <int> <int> <int> <int> <dbl> <int> <int>
1 2013 1 1 517 515 2 830 819
2 2013 1 1 533 529 4 850 830
3 2013 1 1 542 540 2 923 850
4 2013 1 1 544 545 -1 1004 1022
5 2013 1 1 554 600 -6 812 837
6 2013 1 1 554 558 -4 740 728
7 2013 1 1 555 600 -5 913 854
8 2013 1 1 557 600 -3 709 723
9 2013 1 1 557 600 -3 838 846
10 2013 1 1 558 600 -2 753 745
# ℹ 51,945 more rows
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>
Construct filters to isolate:
arrange()
arrange()
reorders the rows: It takes a data frame, and a set of column names (or more complicated expressions) to order by. If you provide more than one column name, each additional column will be used to break ties in the values of preceding columns:
Use desc()
to sort in descending order.
select()
Often you work with large datasets with many columns where only a few are actually of interest to you. select()
allows you to rapidly zoom in on a useful subset using operations that usually only work on numeric variable positions:
You can exclude columns using -
and specify a range using :
.
distinct()
A common use of select()
is to find out which values a set of variables takes. This is particularly useful in conjunction with the distinct()
verb which only returns the unique values in a table.
What do the following data correspond to?
mutate()
As well as selecting from the set of existing columns, it’s often useful to add new columns that are functions of existing columns. This is the job of mutate()
:
# A tibble: 336,776 × 4
flight dep_delay arr_delay gain
<int> <dbl> <dbl> <dbl>
1 1545 2 11 -9
2 1714 4 20 -16
3 1141 2 33 -31
4 725 -1 -18 17
5 461 -6 -25 19
6 1696 -4 12 -16
7 507 -5 19 -24
8 5708 -3 -14 11
9 79 -3 -8 5
10 301 -2 8 -10
# ℹ 336,766 more rows
summarize()
and sample_n()
summarize()
collapses a data frame to a single row based on some function. It’s not very useful yet, but it will be.
sample_n()
provides you with a random sample of rows.
# A tibble: 1 × 1
delay
<dbl>
1 12.6
# A tibble: 10 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
<int> <int> <int> <int> <int> <dbl> <int> <int>
1 2013 3 28 1731 1738 -7 1943 2004
2 2013 12 30 640 645 -5 836 858
3 2013 5 16 1519 1525 -6 1703 1715
4 2013 2 17 2344 2100 164 159 2346
5 2013 8 9 748 752 -4 931 913
6 2013 6 28 846 815 31 1026 1030
7 2013 7 23 2215 2129 46 2400 2326
8 2013 5 11 23 2245 98 126 2357
9 2013 11 5 1548 1550 -2 1655 1710
10 2013 9 10 1125 1130 -5 1317 1334
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>
Mutate the data to create a new column that contains the average speed traveled by the plane for each flight.
Select the new variable and save it, along with tailnum, as a new data frame object.
Mutate the data to create a new column that contains the average speed traveled by the plane for each flight.
Select the new variable and save it, along with tailnum, as a new data frame object.
group_by()
summarize()
and sample_n()
are even more powerful when combined with the idea of “group by”, repeating the operation separately on groups of observations within the dataset.
The group_by()
function describes how to break a dataset down into groups of rows.
group_by()
Find the fastest airplanes in the bunch, measured as the average speed per airplane.
by_tailnum <- group_by(speed_data, tailnum)
avg_speed <- summarize(by_tailnum,
count = n(),
avg_speed = mean(speed, na.rm = TRUE))
arrange(avg_speed, desc(avg_speed))
# A tibble: 4,044 × 3
tailnum count avg_speed
<chr> <int> <dbl>
1 N228UA 1 501.
2 N315AS 1 499.
3 N654UA 1 499.
4 N819AW 1 490.
5 N382HA 26 486.
6 N388HA 36 484.
7 N391HA 21 484.
8 N777UA 1 483.
9 N385HA 28 483.
10 N392HA 13 482.
# ℹ 4,034 more rows
Instead of applying each verb step-by-step, we can chain them into a single data pipeline, connected with the |>
operator. You start the pipeline with a data frame and then pass it to each function in turn.
The pipe syntax (|>
) takes a data frame and sends it to the argument of a function. The mapping goes to the first available argument in the function. For example:
x |> f()
is the same as f(x)
x |> f(y)
is the same as f(x, y)
(better??)
From Hadley Wickham, how to think about tidy data.
Little bunny Foo Foo
Went hopping through the forest
Scooping up the field mice
And bopping them on the head
The nursery rhyme could be created by a series of steps where the output from each step is saved as an object along the way.
Another approach is to concatenate the functions so that there is only one output.
Or even worse, as one line:
Instead, the code can be written using the pipe in the order in which the function is evaluated:
flights2 <- mutate(flights, speed = distance/(air_time/60))
tail_speed <- select(flights2, tailnum, speed)
tail_speed_grp <- group_by(tail_speed, tailnum)
tail_ave <- summarize(tail_speed_grp, number = n(),
avg_speed = mean(speed, na.rm = TRUE))
arrange(tail_ave, desc(avg_speed))
# A tibble: 4,044 × 3
tailnum number avg_speed
<chr> <int> <dbl>
1 N228UA 1 501.
2 N315AS 1 499.
3 N654UA 1 499.
4 N819AW 1 490.
5 N382HA 26 486.
6 N388HA 36 484.
7 N391HA 21 484.
8 N777UA 1 483.
9 N385HA 28 483.
10 N392HA 13 482.
# ℹ 4,034 more rows
flights |>
mutate(speed = distance / (air_time/60)) |>
select(tailnum, speed) |>
group_by(tailnum) |>
summarize(number = n(),
avg_speed = mean(speed, na.rm = TRUE)) |>
arrange(desc(avg_speed))
# A tibble: 4,044 × 3
tailnum number avg_speed
<chr> <int> <dbl>
1 N228UA 1 501.
2 N315AS 1 499.
3 N654UA 1 499.
4 N819AW 1 490.
5 N382HA 26 486.
6 N388HA 36 484.
7 N391HA 21 484.
8 N777UA 1 483.
9 N385HA 28 483.
10 N392HA 13 482.
# ℹ 4,034 more rows
Form a chain that creates a data frame containing only carrier and each carrier’s mean departure delay time. Which carriers have the highest and lowest mean delays?
Form a chain that creates a data frame containing only carrier and the mean departure delay time. Which carriers have the highest and lowest mean delays?
flights |>
group_by(carrier) |>
summarize(avg_delay = mean(dep_delay, na.rm = TRUE)) |>
arrange(desc(avg_delay))
# A tibble: 16 × 2
carrier avg_delay
<chr> <dbl>
1 F9 20.2
2 EV 20.0
3 YV 19.0
4 FL 18.7
5 WN 17.7
6 9E 16.7
7 B6 13.0
8 VX 12.9
9 OO 12.6
10 UA 12.1
11 MQ 10.6
12 DL 9.26
13 AA 8.59
14 AS 5.80
15 HA 4.90
16 US 3.78
Say you’re curious about the relationship between the number of flights that each plane made in 2013, the mean distance that each of those planes flew, and the mean arrival delay. You also want to exclude the edge cases from your analysis, so focus on the planes that have logged more than 20 flights and flown an average distance of less than 2000 miles. Please form the chain that creates this dataset.