#' If tidyverse is not installed:
#install.packages("tidyverse")
#install.packages("nycflights13")
# Load the package containing all the flight data
library(nycflights13)Transform
Practice using the dplyr functions with the nycflights13 data sets
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:
To access one of the data sets:
names (flights) [1] "year" "month" "day" "dep_time"
[5] "sched_dep_time" "dep_delay" "arr_time" "sched_arr_time"
[9] "arr_delay" "carrier" "flight" "tailnum"
[13] "origin" "dest" "air_time" "distance"
[17] "hour" "minute" "time_hour"
To view a description of the variables in a nycflights13 data set:
?nycflights13::flightsFiltering 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.
library(tidyverse)
# Code here...
flights<- nycflights13::flights
nrow(flights)[1] 336776
flights <- flights |>
dplyr::filter(air_time >= 120 & air_time <240)
flights# A tibble: 124,299 × 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 558 -4 740 728
6 2013 1 1 555 600 -5 913 854
7 2013 1 1 557 600 -3 838 846
8 2013 1 1 558 600 -2 753 745
9 2013 1 1 558 600 -2 849 851
10 2013 1 1 558 600 -2 853 856
# ℹ 124,289 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>
nrow(flights)[1] 124299
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.
# Code here...
flights <- flights |>
dplyr::mutate(
date = lubridate::make_date(year, month, day),
dep_time = lubridate::make_datetime(year, month, day, dep_time %/% 100, dep_time %% 100),
arr_time = lubridate::make_datetime(year, month, day, arr_time %/% 100, arr_time %% 100)
)
flights# A tibble: 124,299 × 20
year month day dep_time sched_dep_time dep_delay
<int> <int> <int> <dttm> <int> <dbl>
1 2013 1 1 2013-01-01 05:17:00 515 2
2 2013 1 1 2013-01-01 05:33:00 529 4
3 2013 1 1 2013-01-01 05:42:00 540 2
4 2013 1 1 2013-01-01 05:44:00 545 -1
5 2013 1 1 2013-01-01 05:54:00 558 -4
6 2013 1 1 2013-01-01 05:55:00 600 -5
7 2013 1 1 2013-01-01 05:57:00 600 -3
8 2013 1 1 2013-01-01 05:58:00 600 -2
9 2013 1 1 2013-01-01 05:58:00 600 -2
10 2013 1 1 2013-01-01 05:58:00 600 -2
# ℹ 124,289 more rows
# ℹ 14 more variables: arr_time <dttm>, sched_arr_time <int>, 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>,
# date <date>
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.
#nycflights13::airports|>
#dplyr::filter(stringr::str_detect(name, "Orlando"))
# Code here...
flights <- flights |>
dplyr::filter(carrier %in% c("AA", "DL", "UA") & dest%in% c("MIA", "FLL" , "MCO"))
nrow(flights)[1] 24355
table(flights$carrier)
AA DL UA
8041 9281 7033
flights# A tibble: 24,355 × 20
year month day dep_time sched_dep_time dep_delay
<int> <int> <int> <dttm> <int> <dbl>
1 2013 1 1 2013-01-01 05:42:00 540 2
2 2013 1 1 2013-01-01 06:06:00 610 -4
3 2013 1 1 2013-01-01 06:07:00 607 0
4 2013 1 1 2013-01-01 06:23:00 610 13
5 2013 1 1 2013-01-01 06:44:00 636 8
6 2013 1 1 2013-01-01 06:55:00 700 -5
7 2013 1 1 2013-01-01 06:56:00 659 -3
8 2013 1 1 2013-01-01 06:57:00 700 -3
9 2013 1 1 2013-01-01 06:59:00 700 -1
10 2013 1 1 2013-01-01 07:12:00 715 -3
# ℹ 24,345 more rows
# ℹ 14 more variables: arr_time <dttm>, sched_arr_time <int>, 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>,
# date <date>
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.
# Code here...
flights <- flights |>
dplyr::mutate(
carrier = factor(carrier, levels = c("AA", "DL", "UA"), labels = c("American Airlines", "Delta Air Lines", "United Airlines")),
dest = factor(dest, levels = c("MIA", "FLL", "MCO"), labels = c("Miami", "Fort Lauderdale", "Orlando"))
)
flights# A tibble: 24,355 × 20
year month day dep_time sched_dep_time dep_delay
<int> <int> <int> <dttm> <int> <dbl>
1 2013 1 1 2013-01-01 05:42:00 540 2
2 2013 1 1 2013-01-01 06:06:00 610 -4
3 2013 1 1 2013-01-01 06:07:00 607 0
4 2013 1 1 2013-01-01 06:23:00 610 13
5 2013 1 1 2013-01-01 06:44:00 636 8
6 2013 1 1 2013-01-01 06:55:00 700 -5
7 2013 1 1 2013-01-01 06:56:00 659 -3
8 2013 1 1 2013-01-01 06:57:00 700 -3
9 2013 1 1 2013-01-01 06:59:00 700 -1
10 2013 1 1 2013-01-01 07:12:00 715 -3
# ℹ 24,345 more rows
# ℹ 14 more variables: arr_time <dttm>, sched_arr_time <int>, arr_delay <dbl>,
# carrier <fct>, flight <int>, tailnum <chr>, origin <chr>, dest <fct>,
# air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>,
# date <date>
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.
## Code here
#names(nycflights13::weather)
weather_subset <- nycflights13::weather |>
dplyr::select(origin, time_hour, temp, wind_speed)
weather_subset# A tibble: 26,115 × 4
origin time_hour temp wind_speed
<chr> <dttm> <dbl> <dbl>
1 EWR 2013-01-01 01:00:00 39.0 10.4
2 EWR 2013-01-01 02:00:00 39.0 8.06
3 EWR 2013-01-01 03:00:00 39.0 11.5
4 EWR 2013-01-01 04:00:00 39.9 12.7
5 EWR 2013-01-01 05:00:00 39.0 12.7
6 EWR 2013-01-01 06:00:00 37.9 11.5
7 EWR 2013-01-01 07:00:00 39.0 15.0
8 EWR 2013-01-01 08:00:00 39.9 10.4
9 EWR 2013-01-01 09:00:00 39.9 15.0
10 EWR 2013-01-01 10:00:00 41 13.8
# ℹ 26,105 more rows
flights <- flights |>
dplyr::left_join(weather_subset, by = c("origin", "time_hour"))
flights# A tibble: 24,355 × 22
year month day dep_time sched_dep_time dep_delay
<int> <int> <int> <dttm> <int> <dbl>
1 2013 1 1 2013-01-01 05:42:00 540 2
2 2013 1 1 2013-01-01 06:06:00 610 -4
3 2013 1 1 2013-01-01 06:07:00 607 0
4 2013 1 1 2013-01-01 06:23:00 610 13
5 2013 1 1 2013-01-01 06:44:00 636 8
6 2013 1 1 2013-01-01 06:55:00 700 -5
7 2013 1 1 2013-01-01 06:56:00 659 -3
8 2013 1 1 2013-01-01 06:57:00 700 -3
9 2013 1 1 2013-01-01 06:59:00 700 -1
10 2013 1 1 2013-01-01 07:12:00 715 -3
# ℹ 24,345 more rows
# ℹ 16 more variables: arr_time <dttm>, sched_arr_time <int>, arr_delay <dbl>,
# carrier <fct>, flight <int>, tailnum <chr>, origin <chr>, dest <fct>,
# air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>,
# date <date>, temp <dbl>, wind_speed <dbl>
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()