Transformed data from wide to long format. (+10)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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(scales)
## Warning: package 'scales' was built under R version 4.6.1
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(knitr)
data_path <- file.choose()
data <- read_csv(data_path)
## New names:
## Rows: 5 Columns: 7
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (2): ...1, ...2 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.
## • `` -> `...1`
## • `` -> `...2`
data2 <- data %>% pivot_longer(
cols = !c(1, 2),
names_to = "Cities",
values_to = "Arrivals"
)
data2[6:10,1] <- "ALASKA"
data2[21:25,1] <- "AM WEST"
colnames(data2)[1:2] <- c("Airline","Status")
data3 <- filter(data2, !is.na(Airline))
data3 %>% print(n=20)
## # A tibble: 20 × 4
## Airline Status Cities Arrivals
## <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 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
Submitted data file with at least some count analysis (+45)
Recreated File in Same Format as given, including missing data where there were empty cells in source data. (+5)
Provided code to populate missing data. (+5)
The section shows what I assume are either on-time or delayed arrivals/departures at five U.S. airports from carriers Alaska and AM West (now part of American Airlines). For the sake of this assignment, we will assume they are arrivals.
# Chunk 3 satisfies the count portion of the assignment.
AL_ontime <- data3 %>% filter(Status == "on time" & Airline == "ALASKA") %>% pull(Arrivals) %>% sum()
AMW_ontime <- data3 %>% filter(Status == "on time" & Airline == "AM WEST") %>% pull(Arrivals) %>% sum()
AL_delay <- data3 %>% filter(Status == "delayed" & Airline == "ALASKA") %>% pull(Arrivals) %>% sum()
AMW_delay <- data3 %>% filter(Status == "delayed" & Airline == "AM WEST") %>% pull(Arrivals) %>% sum()
AL_total <- data3 %>% filter(Airline == "ALASKA") %>% pull(Arrivals) %>% sum()
AMW_total <- data3 %>% filter(Airline == "AM WEST") %>% pull(Arrivals) %>% sum()
print(paste0(
"Alaska Airlines saw an average delay percentage of ",
percent(AL_delay / AL_total),
" in flight arrivals whereas AM WEST saw a delay of ",
percent(AMW_delay / AMW_total),
" across all flights on the same five routes."
))
## [1] "Alaska Airlines saw an average delay percentage of 13% in flight arrivals whereas AM WEST saw a delay of 11% across all flights on the same five routes."
print(paste0(
"Alaska Airlines saw ",
AL_delay,
" delayed flights and ",
AL_ontime,
" on-time flights for a grand total of ",
AL_total,
" flights.",
"AM West saw ",
AMW_delay,
" delayed flights and ",
AMW_ontime,
" on-time flights for a grand total of ",
AMW_total,
" flights."
))
## [1] "Alaska Airlines saw 501 delayed flights and 3274 on-time flights for a grand total of 3775 flights.AM West saw 787 delayed flights and 6438 on-time flights for a grand total of 7225 flights."
# Chunk 4 transforms the long dataframe back to a wide dataframe.
data4 <- data3 %>%
pivot_wider(
names_from = Cities,
values_from = Arrivals
)
data4[2,1] <- ""
data4[4,1] <- ""
colnames(data4)[1:2] <- c("","")
data4 <- data4 %>% add_row(.before = 3)
## New names:
## • `` -> `...1`
## • `` -> `...2`
data4
Compared percentage (not just counts) of either delays or arrival rates for two airlines overall. [Either with a chart, or a table; both would be exemplary]. Include text summarizes findings from this comparison. (+10)
Compared percentage (not just counts) of either delays or arrival rates for two airlines across five cities. [Either with a chart, or a table; both would be exemplary]. Include text summarizes findings from this comparison. (+10)
Describe discrepancy between comparing two airlines’ flight performances city-by-city and overall. (+5)
Explain discrepancy between comparing two airlines’ flight performances city-by-city and overall.(+5)
# The following values were calculated in Chunk 3
# AL_ontime
# AMW_ontime
# AL_delay
# AMW_delay
# AL_total
# AMW_total
data5 <- data3 %>%
pivot_wider(
names_from = Status,
values_from = Arrivals
)
data6 <- data5 %>%
select(Airline, Cities)
data5 <- data5 %>%
mutate(
Total = (delayed + `on time`)
)
data6 <- data5 %>%
mutate(
On_Time_Percentage = percent(`on time` / (delayed + `on time`)),
Delayed_Percentage = percent(delayed / (delayed + `on time`))
) %>%
select(Airline, Cities, On_Time_Percentage, Delayed_Percentage)
kable(data5, caption = "Delayed & On Time Arrivals for Alaska & AM West Airlines", align = "c")
| Airline | Cities | on time | delayed | Total |
|---|---|---|---|---|
| ALASKA | Los Angeles | 497 | 62 | 559 |
| ALASKA | Phoenix | 221 | 12 | 233 |
| ALASKA | San Diego | 212 | 20 | 232 |
| ALASKA | San Francisco | 503 | 102 | 605 |
| ALASKA | Seattle | 1841 | 305 | 2146 |
| AM WEST | Los Angeles | 694 | 117 | 811 |
| AM WEST | Phoenix | 4840 | 415 | 5255 |
| AM WEST | San Diego | 383 | 65 | 448 |
| AM WEST | San Francisco | 320 | 129 | 449 |
| AM WEST | Seattle | 201 | 61 | 262 |
kable(data6, caption = "Delayed & On Time Arrival Percentages for Alaska & AM West Airlines", align = "c")
| Airline | Cities | On_Time_Percentage | Delayed_Percentage |
|---|---|---|---|
| ALASKA | Los Angeles | 88.909% | 11.091% |
| ALASKA | Phoenix | 94.850% | 5.150% |
| ALASKA | San Diego | 91.379% | 8.621% |
| ALASKA | San Francisco | 83.140% | 16.860% |
| ALASKA | Seattle | 85.788% | 14.212% |
| AM WEST | Los Angeles | 85.573% | 14.427% |
| AM WEST | Phoenix | 92.103% | 7.897% |
| AM WEST | San Diego | 85.491% | 14.509% |
| AM WEST | San Francisco | 71.269% | 28.731% |
| AM WEST | Seattle | 76.718% | 23.282% |
AM West and Alaska Airlines both serve the Western United States, but from the arrival flight data it appears that their primary focus was on different cities. The majority of Alaska’s flights were to Seattle (2146 arrivals) whereas AM West had the majority of arrivals in Phoenix (5255 arrivals).
Alaska Airlines saw an average delay percentage of 13% in flight arrivals whereas AM WEST saw a delay of 11% across all flights on the same five routes.
Alaska Airlines saw 501 delayed flights and 3274 on-time flights for a grand total of 3775 flights.AM West saw 787 delayed flights and 6438 on-time flights for a grand total of 7225 flights.
AM West was the only airline out of the two that had a delay percentage of total flights greater than 20% (in San Francisco and Seattle). However, these accounted for a smaller proportion of the overall number of flights so overall their performance was still better than Alaska airlines. Alaska, meanwhile, saw a 14.2% delay on arrivals to Seattle but because Seattle accounted for over half of their arrivals, it reduced their overall performance and caused their overall on time percentage to be lower than AM West.
Both airlines saw their best performance in Phoenix, with a 5.1% delay percentage on arrivals for Alaska airlines and a 7.9% delay percentage for AM West.
The pivot_wider() and pivot_longer() functions were helpful in organizing the data for analysis. The exercise clarified the importance of structuring the dataframe in a way that makes analysis efficient by permitting operations across vectors.