In this assignment, I will analyze arrival performance for Alaska Airlines and AM West across five destinations: Los Angeles, Phoenix, San Diego, San Francisco, and Seattle. The original dataset reports the number of flights that arrived on time and the number that were delayed for each airline and destination. My plan is to first recreate the source data in a CSV file using the same wide format shown in the original table. I will then import the data into R and use functions from dplyr and tidyr to clean and transform the dataset. After filling the missing data, I will transform the data from wide to long format.
Finally, I will calculate and compare delay percentages for the two airlines overall and across each of the five destinations. This will allow me to determine whether the overall airline comparison tells the same story as the city-by-city comparison.
library(tidyr)
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)
## Warning: package 'ggplot2' was built under R version 4.4.3
airlines <- data.frame(
Airline = c("Alaska", NA, NA,"AM WEST", NA),
Status = c("on time", "delayed", NA, "on time", "delayed"),
Los_Angeles = c(497, 62, NA, 694, 117),
Phoenix = c(221, 12, NA, 4840, 415),
San_Diego = c(212, 20, NA, 383, 65),
San_Francisco = c(503, 102, NA, 320, 129),
Seattle = c(1841, 305, NA, 201, 61)
)
airlines
## Airline Status Los_Angeles Phoenix San_Diego San_Francisco Seattle
## 1 Alaska on time 497 221 212 503 1841
## 2 <NA> delayed 62 12 20 102 305
## 3 <NA> <NA> NA NA NA NA NA
## 4 AM WEST on time 694 4840 383 320 201
## 5 <NA> delayed 117 415 65 129 61
write.csv(
airlines,
"airline_delays.csv",
row.names = FALSE,
na = ""
)
airlines_wide <- read.csv(
"airline_delays.csv",
na.strings = ""
)
airlines_wide
## Airline Status Los_Angeles Phoenix San_Diego San_Francisco Seattle
## 1 Alaska on time 497 221 212 503 1841
## 2 <NA> delayed 62 12 20 102 305
## 3 <NA> <NA> NA NA NA NA NA
## 4 AM WEST on time 694 4840 383 320 201
## 5 <NA> delayed 117 415 65 129 61
airlines_wide <- airlines_wide %>%
filter(!if_all(everything(), is.na))
airlines_wide<- airlines_wide %>%
fill(Airline, .direction = "down")
airlines_wide
## Airline Status Los_Angeles Phoenix San_Diego San_Francisco Seattle
## 1 Alaska on time 497 221 212 503 1841
## 2 Alaska delayed 62 12 20 102 305
## 3 AM WEST on time 694 4840 383 320 201
## 4 AM WEST delayed 117 415 65 129 61
airlines_long <- airlines_wide %>%
pivot_longer(
cols = Los_Angeles:Seattle,
names_to = "City",
values_to = "Flights"
)
airlines_long
## # A tibble: 20 × 4
## Airline Status City Flights
## <chr> <chr> <chr> <int>
## 1 Alaska on time Los_Angeles 497
## 2 Alaska on time Phoenix 221
## 3 Alaska on time San_Diego 212
## 4 Alaska on time San_Francisco 503
## 5 Alaska on time Seattle 1841
## 6 Alaska delayed Los_Angeles 62
## 7 Alaska delayed Phoenix 12
## 8 Alaska delayed San_Diego 20
## 9 Alaska delayed San_Francisco 102
## 10 Alaska delayed Seattle 305
## 11 AM WEST on time Los_Angeles 694
## 12 AM WEST on time Phoenix 4840
## 13 AM WEST on time San_Diego 383
## 14 AM WEST on time San_Francisco 320
## 15 AM WEST on time Seattle 201
## 16 AM WEST delayed Los_Angeles 117
## 17 AM WEST delayed Phoenix 415
## 18 AM WEST delayed San_Diego 65
## 19 AM WEST delayed San_Francisco 129
## 20 AM WEST delayed Seattle 61
count_summary <- airlines_long %>%
group_by(Airline, Status) %>%
summarise(
Total_Flights = sum(Flights),
.groups = "drop"
)
count_summary
## # A tibble: 4 × 3
## Airline Status Total_Flights
## <chr> <chr> <int>
## 1 AM WEST delayed 787
## 2 AM WEST on time 6438
## 3 Alaska delayed 501
## 4 Alaska on time 3274
I performed a count analysis to calculate the total number of on-time and delayed flights for each airline across all five destinations. Alaska had 3,274 on-time flights and 501 delayed flights, while AM West had 6,438 on-time flights and 787 delayed flights.
overall_delay <- airlines_long %>%
group_by(Airline) %>%
summarise(
Total_Flights = sum(Flights),
Delayed_Flights = sum(Flights[Status == "delayed"]),
Delay_Percentage = Delayed_Flights / Total_Flights * 100
)
overall_delay
## # A tibble: 2 × 4
## Airline Total_Flights Delayed_Flights Delay_Percentage
## <chr> <int> <int> <dbl>
## 1 AM WEST 7225 787 10.9
## 2 Alaska 3775 501 13.3
AM West had a lower percentage of delayed flights than Alaska. Approximately 10.89% of AM West flights were delayed, compared with 13.27% of Alaska flights. Although AM West had more delayed flights in terms of count, it also had more flights in total.
ggplot(overall_delay,
aes(x = Airline, y = Delay_Percentage, fill = Airline)) +
geom_col() +
labs(
title = "Overall Flight Delay Percentage by Airline",
x = "Airline",
y = "Delay Percentage"
) +
theme_minimal()
city_delay <- airlines_long %>%
group_by(Airline, City) %>%
summarise(
Total_Flights = sum(Flights),
Delayed_Flights = sum(Flights[Status == "delayed"]),
Delay_Percentage = Delayed_Flights / Total_Flights * 100,
.groups = "drop"
)
city_delay
## # A tibble: 10 × 5
## Airline City Total_Flights Delayed_Flights Delay_Percentage
## <chr> <chr> <int> <int> <dbl>
## 1 AM WEST Los_Angeles 811 117 14.4
## 2 AM WEST Phoenix 5255 415 7.90
## 3 AM WEST San_Diego 448 65 14.5
## 4 AM WEST San_Francisco 449 129 28.7
## 5 AM WEST Seattle 262 61 23.3
## 6 Alaska Los_Angeles 559 62 11.1
## 7 Alaska Phoenix 233 12 5.15
## 8 Alaska San_Diego 232 20 8.62
## 9 Alaska San_Francisco 605 102 16.9
## 10 Alaska Seattle 2146 305 14.2
The city-by-city comparison shows a different pattern from the overall comparison. Alaska airlines had a lower percentage of delayed flights than AM West in all five destinations. Alaska’s delay percentages were approximately 11.09% in Los Angeles, 5.15% in Phoenix, 8.62% in San Diego, 16.86% in San Francisco, and 14.21% in Seattle. In comparison, AM West’s delay percentages were approximately 14.43%, 7.90%, 14.51%, 28.73%, and 23.28%, respectively. These results show that Alaska had the lower delay percentage within every individual destination.
ggplot(city_delay,
aes(x = City, y = Delay_Percentage, fill = Airline)) +
geom_col(position = "dodge") +
labs(
title = "Flight Delay Percentage by Airline and City",
x = "City",
y = "Delay Percentage",
fill = "Airline"
) +
theme_minimal()
The overall comparison and the city-by-city comparison show different results. Overall, AM West Airlines had a lower delay percentage of approximately 10.89% compared with 13.27% for Alaska. However, when the airlines were compared within each of the five cities, Alaska had a lower delay percentage in every city. This discrepancy occurs because the two airlines did not have the same distribution of flights across the five cities. AM West had a large proportion of its flights in Phoenix, where delay percentages were relatively low. Of AM West’s 7,225 total flights, 5,255 were in Phoenix. Alaska, on the other hand, had only 233 flights in Phoenix and had a large proportion of its flights in Seattle, where delay percentages were higher. Because the overall percentages combine flights from all five cities, these differences in the number of flights to each destination affect the overall results. This demonstrates why looking only at the overall percentage can be misleading. Examining the airlines city by city provides additional information about their flight performance.
This analysis compared flight delays for Alaska and AM West airlines across five cities. Overall, AM West had a lower delay percentage of approximately 10.89%, compared with 13.27% for Alaska. However, the city-by-city analysis showed that Alaska had a lower delay percentage than AM West in all five cities. The difference between the overall and city-level results is due to the unequal distribution of flights across destinations. AM West had a large portion of its flights in Phoenix, where delay percentages were relatively low, while Alaska had a large proportion of its flights in Seattle, where delay percentages were higher. This analysis demonstrates the importance of examining both overall and subgroup percentages when comparing performance, since overall results may not show the full pattern in the data.