Create a .CSV file (or optionally, a MySQL database!) that includes all of the information above.

You’re encouraged to use a “wide” structure so that you can practice tidying and transformations.

Load the libraries

# Load the libraries 
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.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(dplyr)
library(ggplot2)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor

#Create the dataframe

flights <- rbind(c("Airline", "Status", "Los Angeles", "Phoenix", "San Diego", "San Francisco", "Seattle"),
             c("ALASKA", "on time", 497, 221, 212, 503, 1841),
             c(NA, "delayed", 62, 12, 20, 102, 305),
             c("AM WEST", "on time", 694, 4840, 383, 320, 201),
             c(NA, "delayed", 117, 415, 65, 129, 61))

write.table(flights, file = "week5flights.csv", sep = ",", col.names=F, row.names=F)

Read the csv file from github

#Read the csv file 

airlines <- read.csv("https://raw.githubusercontent.com/xhuliaturkaj/week_5/main/week5flights.csv")

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 AM WEST on time         694    4840       383           320     201
## 4    <NA> delayed         117     415        65           129      61

Structuring data

 # First start by filling the Airline Name in the rows where it is missing
airlines <- airlines %>%
  fill(Airline, .direction = "down")
airlines
##   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
#Next use pivot longer to reshape the dataframe
airlines_long <- airlines %>%
  pivot_longer(cols = -c(Airline, Status), names_to = "City", values_to = "Count")
airlines_long
## # A tibble: 20 × 4
##    Airline Status  City          Count
##    <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

Perform analysis to compare the arrival delays for the two airlines.

Let’s begin by investigating the delays for both airlines

# I will summarize the Status counts grouping by Airline

delay_airlines <- airlines_long %>%
  group_by(Airline, Status) %>%
  summarize(Total = sum(Count, na.rm = TRUE)) %>%
  mutate(Proportion = Total / sum(Total))
## `summarise()` has grouped output by 'Airline'. You can override using the
## `.groups` argument.
print(delay_airlines)
## # A tibble: 4 × 4
## # Groups:   Airline [2]
##   Airline Status  Total Proportion
##   <chr>   <chr>   <int>      <dbl>
## 1 ALASKA  delayed   501      0.133
## 2 ALASKA  on time  3274      0.867
## 3 AM WEST delayed   787      0.109
## 4 AM WEST on time  6438      0.891

Next I will visualize the information above

ggplot(delay_airlines, aes(x = Airline, y = Proportion, fill = Status)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
  geom_text(aes(label = percent(Proportion)), 
            position = position_dodge(width = 0.9), vjust = -0.5) +
  labs(y = "Proportion of Flights", 
       title = "Proportion of On-time and Delayed Flights for Each Airline") +
  scale_fill_manual(values = c("on time" = "green", "delayed" = "red"))

The comparison indicates that ALASKA airline has an 86.7% on-time arrival rate and a 13.3% delay rate, whereas AM WEST airline demonstrates a higher on-time arrival rate of 89.1% and a lower delay rate of 10.9%, suggesting AM WEST is relatively more punctual in its services.

Next I would like to explore how these rates vary by city.

city_proportions <- airlines_long %>%
  group_by(Airline, City, Status) %>%
  summarise(Count = sum(Count, na.rm = TRUE), .groups = 'drop') %>%
  group_by(Airline, City) %>%
  mutate(Total = sum(Count), Proportion = Count / Total) %>%
  ungroup()

city_proportions
## # A tibble: 20 × 6
##    Airline City          Status  Count Total Proportion
##    <chr>   <chr>         <chr>   <int> <int>      <dbl>
##  1 ALASKA  Los.Angeles   delayed    62   559     0.111 
##  2 ALASKA  Los.Angeles   on time   497   559     0.889 
##  3 ALASKA  Phoenix       delayed    12   233     0.0515
##  4 ALASKA  Phoenix       on time   221   233     0.948 
##  5 ALASKA  San.Diego     delayed    20   232     0.0862
##  6 ALASKA  San.Diego     on time   212   232     0.914 
##  7 ALASKA  San.Francisco delayed   102   605     0.169 
##  8 ALASKA  San.Francisco on time   503   605     0.831 
##  9 ALASKA  Seattle       delayed   305  2146     0.142 
## 10 ALASKA  Seattle       on time  1841  2146     0.858 
## 11 AM WEST Los.Angeles   delayed   117   811     0.144 
## 12 AM WEST Los.Angeles   on time   694   811     0.856 
## 13 AM WEST Phoenix       delayed   415  5255     0.0790
## 14 AM WEST Phoenix       on time  4840  5255     0.921 
## 15 AM WEST San.Diego     delayed    65   448     0.145 
## 16 AM WEST San.Diego     on time   383   448     0.855 
## 17 AM WEST San.Francisco delayed   129   449     0.287 
## 18 AM WEST San.Francisco on time   320   449     0.713 
## 19 AM WEST Seattle       delayed    61   262     0.233 
## 20 AM WEST Seattle       on time   201   262     0.767

Let’s use city_proportions that I created above to visualize these proportions

ggplot(city_proportions, aes(x = City, y = Proportion, fill = Status)) +
  geom_bar(stat = "identity", position = "dodge", show.legend = TRUE) +
  facet_wrap(~Airline, scales = "free_y") +
  labs(y = "Proportion of Flights",
       x = "City",
       title = "Proportion of On-time and Delayed Flights for Each Airline by City") +
  scale_fill_manual(values = c("on time" = "green", "delayed" = "red")) +
  geom_text(aes(label = scales::percent(Proportion, accuracy = 1)),
            position = position_dodge(width = 0.9), vjust = -0.25) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Conclusion

As we can see from the visual representation there are significant differences in the delay/on-time arrival rates between the two airlines in several cities, with AM WEST typically showing a greater percentage of flights arriving on time, particularly in Phoenix with 95% (on time arrivals) , while Alaska Airlines performs exceptionally well in locations such as Phoenix (92% on time arrivals) and San Diego (85% on time arrivals). San Francisco and Seattle seemed to be cities with a high arrival delay rate for both airlines but specifically for AM WEST. A statistical analysis is essential to determine whether the observed differences in arrival times are statistically significant.