Transform

Practice using the dplyr functions with the nycflights13 data sets

Author

Brian Helsel, PhD

Published

July 1, 2026

Assignment. Practice using the dplyr functions with the nycflights13 data sets. The data sets contained in the nycflights13 package include flights, weather, planes, airports, and airlines.

To access each of the data sets in the nycflights13 R package:

#' If tidyverse is not installed:
#' install.packages("tidyverse")

# Load the package containing all the flight data
library(nycflights13)
library(dplyr)

To access one of the data sets:

flights
# A tibble: 336,776 × 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
# ℹ 336,766 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>

To view a description of the variables in a nycflights13 data set:

?flights

Filtering by Air Time

Filter the flights data set by air_time and assign the results to a data set named flights. Filter the data to retrieve flights that are ≥ 2 hours and < 4 hours.

# Code here...
flights <- flights |>
  dplyr::filter(air_time >= 120 & air_time < 240)

Date and Time Variable

Use the year, month, and day variable in the data set to create a date variable. Use this date variable to modify the dep_time and arr_time to create time stamps.

flights <- flights |>
  dplyr::mutate(
    date = lubridate::make_date(year, month, day),
    dplyr::across(
      c(dep_time, arr_time),
      ~ lubridate::ymd_hm(paste(date, sprintf("%04d", .x)))
    )
  )

Hint: Look at the nycflights13::flights documentation to review the format of the dep_time and arr_time variables. Zero padding can be added with sprintf("%04d", x) where x is the name of the variable.

Filtering by Flight Details

Apply an additional filtering criteria that captures flights to the Orlando, Fort Lauderdale, and Miami airports from American Airlines, Delta Air Lines, and United Airlines.

flights <- flights |>
  dplyr::filter(
    dest %in% c("MCO", "FLL", "MIA") & carrier %in% c("AA", "DL", "UA")
  )

Factor Variables

Create a factor variable for carrier that includes American Airlines, Delta Air Lines, and United Airlines as the labels. Create another factor variable for dest that includes Orlando, Fort Lauderdale, and Miami.

flights <- flights |>
  dplyr::mutate(
    dest = factor(
      dest,
      levels = c("MCO", "FLL", "MIA"),
      labels = c(
        "Orlando",
        "Fort Lauderdale",
        "Miami"
      )
    ),
    carrier = factor(
      carrier,
      levels = c("AA", "DL", "UA"),
      labels = c(
        "American Airlines",
        "Delta Air Lines",
        "United Airlines"
      )
    )
  )

Join Flights with Weather

Join the temp and wind_speed variables from the weather data set to flights. You will need to select these variables first along with the 2 variables that you need as identifers to merge with the flights data set.

flights <- weather |>
  dplyr::select(origin, time_hour, temp, wind_speed) |>
  dplyr::inner_join(flights, by = c("origin", "time_hour"))

Run a Summary Table

Execute the code below to view a table summarizing the destination, temperature, wind speed, time in the air, and distance by Airline carrier. The gtsummary package is excellent for building tables in R for presentations or manuscripts. Converting to a flextable allows more control over formatting for creating publication-ready tables.

# Install the gtsummary package if not already installed
# install.packages("gtsummary")

flights |>
  dplyr::select(carrier, dest, temp, wind_speed, air_time, distance) |>
  gtsummary::tbl_summary(
    by = carrier,
    missing = "no",
    statistic = gtsummary::all_continuous() ~ "{mean} ± {sd}",
    type = distance ~ "continuous",
    label = list(
      dest ~ "Destination",
      temp ~ "Temperature",
      wind_speed ~ "Wind Speed",
      air_time ~ "Time in the Air",
      distance ~ "Distance"
    )
  ) |>
  gtsummary::as_flex_table()

Characteristic

American Airlines
N = 8,0111

Delta Air Lines
N = 9,2471

United Airlines
N = 6,9871

Destination

Orlando

717 (9.0%)

3,490 (38%)

3,089 (44%)

Fort Lauderdale

179 (2.2%)

2,864 (31%)

2,360 (34%)

Miami

7,115 (89%)

2,893 (31%)

1,538 (22%)

Temperature

55 ± 18

57 ± 18

56 ± 18

Wind Speed

11.2 ± 5.6

11.7 ± 5.5

10.1 ± 5.5

Time in the Air

152 ± 12

147 ± 15

146 ± 14

Distance

1,078 ± 42

1,032 ± 66

1,013 ± 68

1n (%); Mean ± SD