library(nycflights13)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
average_arrival <- flights %>%
  filter(!is.na(arr_delay), !is.na(tailnum)) %>%
  group_by(tailnum) %>%
  summarise(average_delay = mean(arr_delay), .groups = "drop")

average_arrival %>%
  slice_min(average_delay, n = 1)
## # A tibble: 1 × 2
##   tailnum average_delay
##   <chr>           <dbl>
## 1 N560AS            -53

Question 5

not_cancelled <- flights %>%
  filter(!is.na(dep_delay), !is.na(arr_delay))

first_last <- not_cancelled %>%
  group_by(year, month, day) %>%
  summarise(first = min(dep_time), last = max(dep_time),
            .groups = "drop") %>%
  arrange(desc(last))

head(first_last)
## # A tibble: 6 × 5
##    year month   day first  last
##   <int> <int> <int> <int> <int>
## 1  2013     2     7    27  2400
## 2  2013     2    11     1  2400
## 3  2013     3    15    11  2400
## 4  2013     3    22    37  2400
## 5  2013     3    25    13  2400
## 6  2013     4     2     9  2400
after_midnight <- flights %>%
  filter(!is.na(dep_delay)) %>%
  mutate(departure_minutes = (sched_dep_time %/% 100) * 60 +
           sched_dep_time %% 100 + dep_delay) %>%
  filter(departure_minutes > 1440)

nrow(after_midnight)
## [1] 1207
head(select(after_midnight, year, month, day, sched_dep_time,
            dep_time, dep_delay))
## # A tibble: 6 × 6
##    year month   day sched_dep_time dep_time dep_delay
##   <int> <int> <int>          <int>    <int>     <dbl>
## 1  2013     1     1           1835      848       853
## 2  2013     1     2           2359       42        43
## 3  2013     1     2           2250      126       156
## 4  2013     1     3           2359       32        33
## 5  2013     1     3           2145       50       185
## 6  2013     1     3           2359      235       156

Question 6

monthly_delays <- flights %>%
  group_by(month) %>%
  summarise(proportion = mean(dep_delay > 60, na.rm = TRUE),
            .groups = "drop") %>%
  mutate(percent = round(100 * proportion, 2))

monthly_delays
## # A tibble: 12 × 3
##    month proportion percent
##    <int>      <dbl>   <dbl>
##  1     1     0.0688    6.88
##  2     2     0.0698    6.98
##  3     3     0.0837    8.37
##  4     4     0.0916    9.16
##  5     5     0.0818    8.18
##  6     6     0.128    12.8 
##  7     7     0.134    13.4 
##  8     8     0.0796    7.96
##  9     9     0.0490    4.9 
## 10    10     0.0469    4.69
## 11    11     0.0402    4.02
## 12    12     0.0942    9.42

Question 7

carriers_by_destination <- flights %>%
  group_by(dest) %>%
  summarise(carriers = n_distinct(carrier), .groups = "drop")

carriers_by_destination %>%
  filter(carriers == max(carriers))
## # A tibble: 5 × 2
##   dest  carriers
##   <chr>    <int>
## 1 ATL          7
## 2 BOS          7
## 3 CLT          7
## 4 ORD          7
## 5 TPA          7

Question 8

The dropdown options were not supplied. These are the corresponding code steps.

1. Group flights by destination.

by_dest <- flights %>%
  group_by(dest)

2. Calculate distance, average arrival delay, and number of flights.

delays <- by_dest %>%
  summarise(
    dist = mean(distance, na.rm = TRUE),
    delay = mean(arr_delay, na.rm = TRUE),
    count = n(),
    .groups = "drop"
  )

3. Keep destinations with more than 20 flights and remove Honolulu.

delays <- delays %>%
  filter(count > 20, dest != "HNL")

4. Plot average arrival delay against distance.

ggplot(delays, aes(x = dist, y = delay)) +
  geom_point(aes(size = count), alpha = 0.5) +
  geom_smooth(se = FALSE, method = "loess") +
  labs(x = "Average distance (miles)",
       y = "Average arrival delay (minutes)",
       size = "Flights") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

Question 9

delays_q9 <- flights %>%
  group_by(dest) %>%
  summarise(
    count = n(),
    dist = mean(distance, na.rm = TRUE),
    delay = mean(arr_delay, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  filter(count > 20, dest != "HNL")

head(delays_q9)
## # A tibble: 6 × 4
##   dest  count  dist delay
##   <chr> <int> <dbl> <dbl>
## 1 ABQ     254 1826   4.38
## 2 ACK     265  199   4.85
## 3 ALB     439  143  14.4 
## 4 ATL   17215  757. 11.3 
## 5 AUS    2439 1514.  6.02
## 6 AVL     275  584.  8.00