Unit 2: Into the Tidyverse
This unit introduces the tidyverse, a family of R packages that share one way of working with data. Using a year of flights from New York’s three airports (the nycflights13 data), you learn the dplyr verbs that filter, sort, select, change, and summarize a data frame, chained together with the pipe. The unit ends by turning those tables into graphs with ggplot2.
Chapter 6: Into the Tidyverse: glimpse, filter, and the Pipe
This chapter introduces the tidyverse and the data science workflow it is built around. You look at a data frame with glimpse(), keep the rows you want with filter(), and chain steps together with the pipe, |>. It also explains missing values (NA) and how they affect filtering.
Chapter 7: Into the Tidyverse: arrange, distinct, select, and mutate
dplyr verbs work on either rows or columns. This chapter sorts rows with arrange(), finds unique values with distinct(), keeps the columns you need with select(), and calculates new columns with mutate(). Sorting a new column turns out to be a quick way to spot outliers worth checking.
Chapter 8: Into the Tidyverse: summarize, group_by, and drop_na
summarize() collapses many rows into summary statistics, and group_by() makes it give one answer for each group, such as each month or each airline. The chapter compares two ways of handling missing values, na.rm = TRUE and drop_na(), and shows why the average of averages can mislead and how a weighted average fixes it.
Chapter 9: Into the Tidyverse: Graphs with ggplot2
ggplot2 builds graphs from the grammar of graphics: data, aesthetics that map columns to visual properties, and geoms that draw the marks, added in layers with +. This chapter makes scatterplots, bar charts, and boxplots, explains the difference between setting and mapping an aesthetic, splits plots into small multiples with facets, and finishes plots with colors, labels, themes, and ggsave().
install.packages(), then load it in
each session with library().
An R package of data on every flight that left New York City’s three
airports in 2013, including the flights data frame.
The tidyverse package for transforming data frames: filtering rows,
choosing columns, adding new columns, joining tables, and summarizing.
R’s table: rows are observations and columns are variables. A tibble is
the tidyverse’s version of a data frame.
The operator |>, which passes the result on its left as
the first argument of the function on its right. Read it as “and then.”
The tidyverse package for making plots and maps by adding layers.
The steps of a data analysis: import, tidy, then a loop of transform,
visualize, and model, and finally communicate.
A value that is unknown, written NA (“not available”) in R.
Most calculations with NA give NA; test for it
with is.na().
A value counted once, however many times it repeats.
distinct() keeps one row for each unique value, or each
unique combination of values.
A value far from all the others. It may be real or an error, so check it
before you report it.
One number that describes many values, such as their mean, median,
maximum, or count. summarize() calculates them.
An average in which some values count more than others. Total distance
divided by total air time weights each flight’s speed by its air time.
The idea that any chart can be described with the same small set of
parts: data, aesthetic mappings, geoms, scales, facets, and coordinates.
ggplot2 is built on it.