Assignment 5A: Airline Delays

Author

Mubin Ejaz

Approach

I had decided to read in the image and extract data to continue but ended up wasting so much time.

At the end I did have to take ChatGPT’s advice and just save the data in a csv and read it. That way it was much cleaner to read, plot, analyse, and conclude.

Code Chunk I tried

library(knitr)
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(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.1     ✔ readr     2.2.0
✔ ggplot2   4.0.3     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.1     ✔ tidyr     1.3.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
library(tesseract)
df2<-read_csv("5A.csv")
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.
airline_tidy <- df2 |>
  pivot_longer(
    cols = `Los Angeles`:Seattle,
    names_to = "Destination",
    values_to = "Flights"
  )
airline_tidy
# A tibble: 20 × 4
   Airline Status  Destination   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
 7 ALASKA  delayed Phoenix            12
 8 ALASKA  delayed San Diego          20
 9 ALASKA  delayed San Francisco     102
10 ALASKA  delayed Seattle           305
11 AMWEST  on time Los Angeles       694
12 AMWEST  on time Phoenix          4840
13 AMWEST  on time San Diego         383
14 AMWEST  on time San Francisco     320
15 AMWEST  on time Seattle           201
16 AMWEST  delayed Los Angeles       117
17 AMWEST  delayed Phoenix           415
18 AMWEST  delayed San Diego          65
19 AMWEST  delayed San Francisco     129
20 AMWEST  delayed Seattle            61
# Using pivot_longer seemed easier to manipulate the data. 
totals <- airline_tidy |>
  group_by(Airline, Destination) |>
  summarise(
    Total = sum(Flights),
    .groups = "drop"
  )
totals
# A tibble: 10 × 3
   Airline Destination   Total
   <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 AMWEST  Los Angeles     811
 7 AMWEST  Phoenix        5255
 8 AMWEST  San Diego       448
 9 AMWEST  San Francisco   449
10 AMWEST  Seattle         262
delays <- airline_tidy |>
  filter(Status == "delayed")
delay_rates <- airline_tidy |>
  group_by(Airline, Destination) |>
  summarise(
    Total = sum(Flights),
    Delayed = sum(Flights[Status == "delayed"]),
    Delay_Rate = Delayed / Total,
    .groups = "drop"
  )
overall <- airline_tidy |>
  group_by(Airline) |>
  summarise(
    Total_Flights = sum(Flights),
    Delayed_Flights = sum(Flights[Status == "delayed"]),
    Delay_Rate = Delayed_Flights / Total_Flights * 100,
    .groups = "drop"
  )
ggplot(delay_rates,
       aes(x = Destination, y = Delay_Rate, fill = Airline)) +
  geom_col(position = "dodge") +
  labs(
    title = "Arrival Delay Rates by Airline and Destination",
    x = "Destination",
    y = "Delay Rate (%)"
  )

write_csv(overall, "output_5A.csv")

Simple data extraction and outputting to FlightDelays.csv gave me a lots of untidy data. All the data has been extracted to one single column and second and third column is just garbage (probably image formatting.

Analysis:

I grouped the data by destination and airlines. Calculated the total number of flights. Also calculated the delayed flights. That helped me calculate the delay rate (# of delayed flights/total number of flights). I did need LLM help in doing so, as I had already taken so long and wasted time in trying to extract and manipulate the image.