library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
url = r"(https://raw.githubusercontent.com/KanizSanzida/DATA607/main/Flightdata.csv)"
suppressMessages(
df <- read_csv(url, skip_empty_rows = TRUE, show_col_types = FALSE)[-3,] # Skip_empty_rows does not work here and I'm not sure why, as we can see the third row is empty and gets converted to NAs as if skip_empty_rows = FALSE
)
names(df)[1:2] <- c("Airline", "Delay Status")
df
df2 <- df |>
fill(Airline) |>
pivot_longer(cols=3:7,names_to= "City",values_to = "Count")
glimpse(df2)
## Rows: 20
## Columns: 4
## $ Airline <chr> "ALASKA", "ALASKA", "ALASKA", "ALASKA", "ALASKA", "ALAS…
## $ `Delay Status` <chr> "on time", "on time", "on time", "on time", "on time", …
## $ City <chr> "Los Angeles", "Phoenix", "San Diego", "San Francisco",…
## $ Count <dbl> 497, 221, 212, 503, 1841, 62, 12, 20, 102, 305, 694, 48…
df2 |>
group_by(Airline, `Delay Status`) |>
summarize("Flights" = sum(Count), .groups= "drop_last") |>
mutate("Proportion of Flights" = Flights/sum(Flights)) |>
knitr::kable()
| Airline | Delay Status | Flights | Proportion of Flights |
|---|---|---|---|
| ALASKA | delayed | 501 | 0.1327152 |
| ALASKA | on time | 3274 | 0.8672848 |
| AM WEST | delayed | 787 | 0.1089273 |
| AM WEST | on time | 6438 | 0.8910727 |
ggplot(df2, aes(x=Airline,y=Count,fill = `Delay Status`)) +
geom_bar(stat="identity", position = position_fill(reverse= TRUE)) +
facet_wrap(~City) +
labs(title = "Flight Delays Per City", y = "Proportion of Flights") +
theme(legend.position = c(.85,.25), plot.title = element_text(hjust = 0.5))
As a conclusion, Utilizing our faceted bar plot, we can now assess the preferable airline choice for each city. it appears that, in every city, the proportion of delayed flights is consistently lower for Alaskan Airlines compared to AM West. This stark deviation from the overall trend of delays in each city suggests that the logical next step in this data analysis would be to gather more data. The current disparity in cases between Alaskan Airlines and AM West is generating mixed signals, a situation likely to be rectified with a more extensive dataset.