5A Flight Delays

Introduction

Starting this project, we need to recreate the given data as a dataframe. This will create a “wide” dataframe that needs to be transposed into a “tall” format for analysis.

library(tidyverse)
library(gt)
library(ggplot2)

flights <- data.frame(
  Airline = c("ALASKA", "ALASKA", "AM WEST", "AM WEST"),
  Status  = c("on_time", "delayed", "on_time", "delayed"),
  `Los Angeles`   = c(497, 62, 694, 117),
  Phoenix         = c(221, 12, 4840, 415),
  `San Diego`     = c(212, 20, 383, 65),
  `San Francisco` = c(503, 102, 320, 129),
  Seattle         = c(1841, 305, 201, 61),
  check.names = FALSE
)

write.csv(flights, "flight_delays.csv")

Tidying the data

In order to flip the data into a taller style dataframe, we can use the pivot longer from the tidyr package. In much the same way that excel creates a pivot table, this will give use another dataframe that breaks this up the information into the Ariline, Status, City, and number of flights.

tidy_flights <- pivot_longer(
  flights, cols = -c(Airline, Status), 
  names_to = "City", values_to = "Flights" )

tidy_flights |> 
  gt() |>
  tab_header(title = "Flight info")
Flight info
Airline Status City Flights
ALASKA on_time Los Angeles 497
ALASKA on_time Phoenix 221
ALASKA on_time San Diego 212
ALASKA on_time San Francisco 503
ALASKA on_time Seattle 1841
ALASKA delayed Los Angeles 62
ALASKA delayed Phoenix 12
ALASKA delayed San Diego 20
ALASKA delayed San Francisco 102
ALASKA delayed Seattle 305
AM WEST on_time Los Angeles 694
AM WEST on_time Phoenix 4840
AM WEST on_time San Diego 383
AM WEST on_time San Francisco 320
AM WEST on_time Seattle 201
AM WEST delayed Los Angeles 117
AM WEST delayed Phoenix 415
AM WEST delayed San Diego 65
AM WEST delayed San Francisco 129
AM WEST delayed Seattle 61

Continuing the analysis, we can look at the total percentage of the flights that were on time or delayed as well as the total number of flights that each airline handled. Looking at the totals, we can see that AM West had nearly double the amount of flights as Alaska. In addition, Alaska had more flights delayed as a percentage of total flights.

by_city <- tidy_flights |>
  pivot_wider(names_from = Status, values_from = Flights) |>
  mutate(Total = `on_time` + delayed,
         Pct_on_time = round(100 * `on_time` / Total, 2),
         Pct_delayed = round(100 * delayed / Total, 2))
by_city |> 
  gt() |> 
  tab_header(title = "Percentage On Time By City")
overall <- by_city |>
  group_by(Airline) |>
  summarise(on_time = sum(`on_time`), delayed = sum(delayed),
            Total = on_time + delayed,
            Pct_on_time = round(100 * on_time / Total, 2),
            Pct_delayed = round(100 * delayed / Total, 2))

overall |>
  gt() |> 
  tab_header(title = "Overall")
Percentage On Time By City
Airline City on_time delayed Total Pct_on_time Pct_delayed
ALASKA Los Angeles 497 62 559 88.91 11.09
ALASKA Phoenix 221 12 233 94.85 5.15
ALASKA San Diego 212 20 232 91.38 8.62
ALASKA San Francisco 503 102 605 83.14 16.86
ALASKA Seattle 1841 305 2146 85.79 14.21
AM WEST Los Angeles 694 117 811 85.57 14.43
AM WEST Phoenix 4840 415 5255 92.10 7.90
AM WEST San Diego 383 65 448 85.49 14.51
AM WEST San Francisco 320 129 449 71.27 28.73
AM WEST Seattle 201 61 262 76.72 23.28
Overall
Airline on_time delayed Total Pct_on_time Pct_delayed
ALASKA 3274 501 3775 86.73 13.27
AM WEST 6438 787 7225 89.11 10.89

Final comparison

Alaska is on time more often to every city while also having fewer flights in the majority of cities. When looking at the raw data it does not paint the picture of how many more flights AM West has to Phoenix. The vast majority of flight are with AM West to Phoenix and yet, with the larger sample size we see that AM West still has a lower percentage of on time flights across the board.

by_city_pct <- by_city |> select(Airline, City, Pct_on_time) |>
  pivot_wider(names_from = Airline, values_from = Pct_on_time)

by_city_pct |> 
  gt() |>
  tab_header(title="Percetages")
ggplot(by_city, aes(x = City, y = Pct_on_time, color = Airline, group = Airline)) +
  geom_line() +
  geom_point(size = 1) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    x = "City",
    y = "Percent on Time",
    title = "Airlines Percentage of Flights on Time"
  )
ggplot(by_city, aes( x = City, y = on_time, color = Airline, group = Airline, fill = Airline)) + 
  geom_col(position = "dodge") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
  labs( 
    x = "City",
    y = "On Time Flights", 
    title = "Total On Time Flights")
Percetages
City ALASKA AM WEST
Los Angeles 88.91 85.57
Phoenix 94.85 92.10
San Diego 91.38 85.49
San Francisco 83.14 71.27
Seattle 85.79 76.72

Conclusion

The percentage of flights that are delayed is heavily correlated with each other between the two airlines. The most likely reason for this will come from conditions both airlines will have to work around such as weather. Alaska has generally fewer flights which would make each delay have more weight for Alaska, however we see that they maintain a higher percentage of on time flights. Phoenix is a massive outlier for AM West, while Seattle is an outlier for Alaska. There is not enough information to identify why AM West is consistently less punctual than Alaska. The next step would need to be gathering information on the delays themselves to compare the reasoning and dates of flights. Directions for this further analysis would include fleet size and ability to cover plane switches, and the maintenance schedules.