For this assignment, I will begin by recreating the provided airline delay dataset while preserving any missing values from the original data. I will then use R to identify and populate the missing data as appropriate. After preparing the dataset, I will transform it from wide format to long format so that the flight status information can be analyzed more easily.
Next, I will perform a count analysis and calculate the percentage of delayed flights for each airline, and review the city-by-city results and explain how differences in flight volume or grouping may affect the interpretation of airline performance.
Introduction
This assignment analyzes arrival delay data for Alaska Airlines and AM West across five destinations: Los Angeles, Phoenix, San Diego, San Francisco, and Seattle. The purpose of the analysis is to practice tidying and transforming data in R while comparing airline performance using flight delay percentages. The analysis will examine both overall airline performance and performance within each individual destination.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
The original source contained two blank airline labels in the delayed rows. These were represented as NA values and then populated using fill() so that each observation contained an airline name.
# A tibble: 4 × 3
Airline Status Total
<chr> <chr> <dbl>
1 ALASKA delayed 501
2 ALASKA on time 3274
3 AM WEST delayed 787
4 AM WEST on time 6438
Here I Start with airline_long, group by airline and status, and add all counts together.
airline_long |>group_by(Airline, Status)
# A tibble: 20 × 4
# Groups: Airline, Status [4]
Airline Status City Count
<chr> <chr> <chr> <dbl>
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
# A tibble: 2 × 3
Airline Total_Flights Delayed_Flights
<chr> <dbl> <dbl>
1 ALASKA 3775 501
2 AM WEST 7225 787
Here we calculate the results separately for ALASKA and AM WEST, sum all on-time and delayed flights together for each airline, and setting up a row for total delays.
The overall comparison shows that ALASKA had a higher percentage of delayed flights than AM WEST. ALASKA’s delay rate was approximately 13%, while AM WEST’s was approximately 11%. Based on the aggregated data, AM WEST had the lower overall delay percentage.
# A tibble: 10 × 5
Airline City Total_Flights Delayed_Flights Delay_Percent
<chr> <chr> <dbl> <dbl> <dbl>
1 ALASKA Los Angeles 559 62 11.1
2 ALASKA Phoenix 233 12 5.15
3 ALASKA San Diego 232 20 8.62
4 ALASKA San Francisco 605 102 16.9
5 ALASKA Seattle 2146 305 14.2
6 AM WEST Los Angeles 811 117 14.4
7 AM WEST Phoenix 5255 415 7.9
8 AM WEST San Diego 448 65 14.5
9 AM WEST San Francisco 449 129 28.7
10 AM WEST Seattle 262 61 23.3
This gives us one result for every airline-city combination.
# A tibble: 5 × 3
City ALASKA `AM WEST`
<chr> <dbl> <dbl>
1 Los Angeles 11.1 14.4
2 Phoenix 5.15 7.9
3 San Diego 8.62 14.5
4 San Francisco 16.9 28.7
5 Seattle 14.2 23.3
When the airlines are compared city by city, ALASKA has a lower delay percentage than AM WEST in all five destinations. The largest differences appear in San Francisco and Seattle, where AM WEST has substantially higher delay percentages. These results differ from the overall comparison, which showed AM WEST with the lower overall delay rate.
# A tibble: 10 × 3
Airline City Total_Flights
<chr> <chr> <dbl>
1 ALASKA Los Angeles 559
2 ALASKA Phoenix 233
3 ALASKA San Diego 232
4 ALASKA San Francisco 605
5 ALASKA Seattle 2146
6 AM WEST Los Angeles 811
7 AM WEST Phoenix 5255
8 AM WEST San Diego 448
9 AM WEST San Francisco 449
10 AM WEST Seattle 262
Simpson’s Paradox
The overall comparison and the city-by-city comparison lead to different conclusions. AM WEST has a lower delay percentage than ALASKA. However, when each city is examined separately, ALASKA has the lower delay percentage in all five destinations. This difference occurs because the airlines operate very different numbers of flights in each city. Cities with larger flight volumes have more influence on the overall percentage, so the combined results can differ from the pattern observed within each individual city.
Conclusion
This analysis showed how the structure and grouping of data can affect interpretation. After recreating the dataset, filling the missing airline labels, and transforming the data from wide to long format, delay percentages were calculated overall and by city. Although AM WEST had the lower overall delay percentage, ALASKA had the lower delay percentage in every individual city. The difference was caused by the unequal distribution of flight volumes across destinations, demonstrating why both aggregate and subgroup-level results should be examined before drawing conclusions.