LOADING MY LIBRARIES

library(readr)
library(dplyr)
## 
## 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(tidyr)
library(ggplot2)

IMPORTING THE DATA

airline_data <- read_csv(file.choose())
## Rows: 4 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): airline, status
## dbl (5): los_angeles, phoenix, san_diego, san_francisco, seattle
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(airline_data)
## # A tibble: 4 × 7
##   airline status  los_angeles phoenix san_diego san_francisco seattle
##   <chr>   <chr>         <dbl>   <dbl>     <dbl>         <dbl>   <dbl>
## 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
glimpse(airline_data)
## Rows: 4
## Columns: 7
## $ airline       <chr> "Alaska", "Alaska", "AM_West", "AM_West"
## $ status        <chr> "On_Time", "Delayed", "On_Time", "Delayed"
## $ los_angeles   <dbl> 497, 62, 694, 117
## $ phoenix       <dbl> 221, 12, 4840, 415
## $ san_diego     <dbl> 212, 20, 383, 65
## $ san_francisco <dbl> 503, 102, 320, 129
## $ seattle       <dbl> 1841, 305, 201, 61

CHECKING FOR MISSING DATA

colSums(is.na(airline_data))
##       airline        status   los_angeles       phoenix     san_diego 
##             0             0             0             0             0 
## san_francisco       seattle 
##             0             0

CONVERTING THE DATA COLUMNS

airline_long <- airline_data %>%
  pivot_longer(
    cols = los_angeles:seattle,
    names_to = "city",
    values_to = "flights"
  )

## DATA TRANSFORMED FROM WIDE TO LONG FORMAT

head(airline_long)
## # A tibble: 6 × 4
##   airline status  city          flights
##   <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

COUNT ANALYSIS

flight_counts <- airline_long %>%
  group_by(airline, status) %>%
  summarise(
    total_flights = sum(flights),
    .groups = "drop"
  )

flight_counts
## # A tibble: 4 × 3
##   airline status  total_flights
##   <chr>   <chr>           <dbl>
## 1 AM_West Delayed           787
## 2 AM_West On_Time          6438
## 3 Alaska  Delayed           501
## 4 Alaska  On_Time          3274

CALCULATE OVERALL DELAY PERCENTAGE

overall_delay <- airline_long %>%
  group_by(airline) %>%
  summarise(
    total_flights = sum(flights),
    delayed_flights = sum(flights[status == "Delayed"]),
    delay_percentage = round(
      delayed_flights / total_flights * 100,
      2
    ),
    .groups = "drop"
  )

overall_delay
## # A tibble: 2 × 4
##   airline total_flights delayed_flights delay_percentage
##   <chr>           <dbl>           <dbl>            <dbl>
## 1 AM_West          7225             787             10.9
## 2 Alaska           3775             501             13.3
## Alaksa airline has a higher overall % delay (13.27%) compared to AM_West(10.89%), however considering the fact that AM_west has had 3,450 more flights than Alaska and still manage for have a lower delay % is outstanding. Alaska has a lot to learn from AM_West

VISUALIZATION

ggplot(
  overall_delay,
  aes(x = airline, y = delay_percentage)
) +
  geom_col() +
  labs(
    title = "Overall Flight Delay Percentage by Airline",
    x = "Airline",
    y = "Delay Percentage"
  ) +
  theme_minimal()

## "The overall comparison shows that Alaska had a higher percentage of delayed flights than AM West. Alaska had approximately 13.27% of flights delayed, compared with approximately 10.89% for AM West. I used percentages instead of only comparing the number of delayed flights because the two airlines had different total numbers of flights."

AIRLINE COMPARISM ACROSS CITIES

city_delay <- airline_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>                 <dbl>           <dbl>            <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
## 

CITY TO CITY COMPARISON GRAPH

ggplot(
  city_delay,
  aes(
    x = city,
    y = delay_percentage,
    fill = airline
  )
) +
  geom_col(position = "dodge") +
  labs(
    title = "Flight Delay Percentage by City and Airline",
    x = "City",
    y = "Delay Percentage",
    fill = "Airline"
  ) +
  theme_minimal()

## Ok i have learnt here not to conclude on analysis when i havent gone indepth, the city to city comparism makes it clear alaska isnt doing very bad. the graph makes it very clear that alaska out-performs AM_West. AM_West overall better % peformance can be attributed to Phoenix total flights (5255) while alaska records (233) for phoenix. That is a (5,022) discrepancy.

FURTHER ANALYSIS ON DISCREPANCY

city_comparison <- city_delay %>%
  select(airline, city, delay_percentage) %>%
  pivot_wider(
    names_from = airline,
    values_from = delay_percentage
  )

city_comparison
## # A tibble: 5 × 3
##   city          AM_West Alaska
##   <chr>           <dbl>  <dbl>
## 1 los_angeles     14.4   11.1 
## 2 phoenix          7.90   5.15
## 3 san_diego       14.5    8.62
## 4 san_francisco   28.7   16.9 
## 5 seattle         23.3   14.2
## alaska actually outperforms AM_West in city to city comparisms.

EXPLANATION TO DISCREPANCY

## overall it was observed that AM_West had a better % delay than Alaska. And one crucial lesson i learnt today is not to conclude on general or surface analysis when you have not gone in-depth. Because Upon in-depth analysis (city to city) i discovered that i had it all wrong. Alaska Airline outperformed AM_West in every individual city. So i had one major question and that was to try to understand why Alaska airlines have an overall poor % delay. Line 103 solved the puzzle. AM West has a particularly large number of flights in Phoenix, which has a relatively lower delay percentage for AM West. This difference in flight distribution affects the overall calculation and causes the overall result to differ from the individual city comparisons.