Given the chart below, we were tasked with performing analysis to compare arrival delays from the two airlines
This required creating a .csv file of the information. It resulted in a messy wide structured dataframe when loaded into R. After transforming and tidying the data, we could compare the arrival delays by airline and by city using tables and charts.
Load in Data
The raw data is loaded in from an excel-created .csv file, hosted on github.
X X.1 Los.Angeles Phoenix San.Diego San.Fransisco Seattle
1 ALASKA on time 497 221 212 503 1841
2 delayed 62 12 20 102 305
3 NA NA NA NA NA
4 AM WEST on time 694 4840 383 320 201
5 delayed 117 415 65 129 61
Tidy Data
Before any analysis can be performed, the flight data must be tidied up. In the raw form, it is in a wide format, with rows of NA’s, column name convention issues, and missing airline information. First the columns are renamed, the airline names are filled in, the NA row is removed, the data is converted into a long format, and finally the city names are edited for convention.
Code
# Rename columnsdf_clean <- df_raw %>%rename(airline = X, status = X.1) # Fill in missing airlinesdf_clean[2, "airline"] <-"ALASKA"df_clean[5, "airline"] <-"AM WEST"# Remove empty rowdf_clean <- df_clean[-3, ] # Transform into long format df_long <- df_clean %>%pivot_longer(cols =-c(airline, status),names_to ="city",values_to ="flight_count")# Clean up city namesdf_long <- df_long %>%mutate(city =gsub("\\.", " ", city))
Calculations
We calculate each airline’s percentage of delays overall and per city.
Code
# Calculate df with delays per city, including breakout per airlinedf_city <- df_long %>%group_by(airline, city) %>%summarise(total_flights =sum(flight_count),delayed_flights =sum(flight_count[status =="delayed"]),delay_pct = (delayed_flights / total_flights)*100,.groups ="drop" )# Calculate df with delays per airlinedf_airline <- df_long %>%group_by(airline) %>%summarise(city ="Overall",total_flights =sum(flight_count),delayed_flights =sum(flight_count[status =="delayed"]),delay_pct = (delayed_flights / total_flights)*100,.groups ="drop" )# Calculate df with delays per city, NO breakout per airlinedf_city_combined <- df_long %>%group_by(city) %>%summarise(total_flights =sum(flight_count),delayed_flights =sum(flight_count[status =="delayed"]),delay_pct = (delayed_flights / total_flights) *100,.groups ="drop" ) %>%arrange(desc(delay_pct)) df_overall <-bind_rows(df_city, df_airline)
Delay percentage broken down by city and airline is then displayed in a chart format using a great table, as well as a bar plot. The same information is also shown grouped just per city with the airline data combined.
It is clear from the visualized data that although both airlines trend together for each city, AM West consistently experiences more delays. Overall, AM West has about 3% more delays than Alaska. For both airlines, San Francisco had the highest delay rate, at almost 22%.
Because these are all arrival delays, I would be interested in exploring the associated departure delays from the airports each plane is leaving from. This would help us determine if the planes are arriving late because they are departing late or if their is something in the typical route that causes the delay. We could also expand the analysis by pulling in information from other cities, airline, and years.