##Introduction

For this assignment, I will analyze airline arrival performance data for Alaska Airlines and AmWest across five destinations. The data is currently presented in a wide format, so my first step will be to recreate the table as a CSV file and upload it to GitHub. After importing the data into R, I will use the tidyr and dplyr packages to transform the dataset into a tidy format for easier analysis.Once the data has been cleaned and organized, I will calculate delay and on-time percentages for each airline. I will compare the airlines both overall and by individual city to determine which airline has the better arrival performance. I also plan to create tables and visualizations to help illustrate the results and identify any patterns in the data.

My approach for this assignments is to first create a CSV file from the airline delay data provided, and then I will recreate the table in the same format as shown, including all airlines, destinations, and arrival status counts. After creating the CSV file, I will upload it to my GitHub repository so that the data file is stored in an accessible location and can be referenced in my project. Next, I will import the CSV file into R and examine the dataset’s structure. Once the data has been loaded, I will use the tidyr and dplyr packages to clean and transform the data. Specifically, I will convert the dataset from a wide format into a tidy long format using pivot_longer, making it easier to analyze.After tidying the data, I will calculate the total number of flights, delayed flights, and on-time flights for each airline. I will then compute delay percentages and compare the overall performance of Alaska Airlines and AmWest. Following the overall comparison, I will analyze the delay percentages for each destination city to assess how the airlines perform at each location. To better present the results, I will create tables and visualizations showing the delay rates by airline and destination. And then I will summarize my findings and discuss any differences between the overall airline performance and the city-by-city results. I will explain why these differences occur and what they reveal about the data. Once the analysis is complete, I will publish the R Markdown file to RPubs and provide links to both my GitHub repository and RPubs submission.

One challenge I anticipate is correctly transforming the data from its original wide format to a tidy long format while preserving the relationships among airlines, flight status, and destinations. Another challenge may be interpreting the results if the overall airline rankings differ from the city-by-city comparisons, since this requires careful analysis of percentages rather than simply comparing raw counts.

Original structure

The original dataset is stored in a wide format. Each row represents an airline-arrival-status combination, while each city is represented by a separate column. Although this format is easy for humans to read, it is not ideal for analysis because city names are stored as column headers rather than observations.

library(readr)
airline <- read_csv("https://raw.githubusercontent.com/lioneljr17/LDATA607/refs/heads/main/607LAB5A/airline_table.csv")
## Rows: 4 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Airline, Status
## dbl (5): LosAngeles, Phoenix, SanDiego, SanFrancisco, 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.
View(airline)

tarnforming wide to long

To create a tidy dataset, I used the pivot_longer() function from the tidyr package. This transformation converts the city columns into a single City variable and places the corresponding counts into a Count variable. The resulting dataset contains one observation per row and is easier to summarize and visualize.

airline_long <- airline %>%
  pivot_longer(cols =LosAngeles:Seattle,
                names_to ="city",
                values_to ="Count")

airline_long
## # A tibble: 20 Ă— 4
##    Airline Status  city         Count
##    <chr>   <chr>   <chr>        <dbl>
##  1 ALASKA  On time LosAngeles     497
##  2 ALASKA  On time Phoenix        221
##  3 ALASKA  On time SanDiego       212
##  4 ALASKA  On time SanFrancisco   503
##  5 ALASKA  On time Seattle       1841
##  6 ALASKA  Delayed LosAngeles      62
##  7 ALASKA  Delayed Phoenix         12
##  8 ALASKA  Delayed SanDiego        20
##  9 ALASKA  Delayed SanFrancisco   102
## 10 ALASKA  Delayed Seattle        305
## 11 AMWEST  On time LosAngeles     694
## 12 AMWEST  On time Phoenix       4840
## 13 AMWEST  On time SanDiego       383
## 14 AMWEST  On time SanFrancisco   320
## 15 AMWEST  On time Seattle        201
## 16 AMWEST  Delayed LosAngeles     117
## 17 AMWEST  Delayed Phoenix        415
## 18 AMWEST  Delayed SanDiego        65
## 19 AMWEST  Delayed SanFrancisco   129
## 20 AMWEST  Delayed Seattle         61
city_summary <- airline_long %>%
  group_by(Airline,city)%>%
  summarize (
    total = sum(Count),
    Delayed = Count[Status =="Delayed"],
    DelayedRate = round(100*Delayed/total,2),
    .groups ="drop"
  )

city_summary
## # A tibble: 10 Ă— 5
##    Airline city         total Delayed DelayedRate
##    <chr>   <chr>        <dbl>   <dbl>       <dbl>
##  1 ALASKA  LosAngeles     559      62       11.1 
##  2 ALASKA  Phoenix        233      12        5.15
##  3 ALASKA  SanDiego       232      20        8.62
##  4 ALASKA  SanFrancisco   605     102       16.9 
##  5 ALASKA  Seattle       2146     305       14.2 
##  6 AMWEST  LosAngeles     811     117       14.4 
##  7 AMWEST  Phoenix       5255     415        7.9 
##  8 AMWEST  SanDiego       448      65       14.5 
##  9 AMWEST  SanFrancisco   449     129       28.7 
## 10 AMWEST  Seattle        262      61       23.3
overall_summary <- airline_long %>%
  group_by(Airline,city)%>%
  summarize (
    total = sum(Count),
    Delayed = Count[Status =="Delayed"],
    DelayedRate = round(100*Delayed/total,2),
    .groups ="drop"
  )
names(airline_long)
## [1] "Airline" "Status"  "city"    "Count"
head(airline_long)
## # A tibble: 6 Ă— 4
##   Airline Status  city         Count
##   <chr>   <chr>   <chr>        <dbl>
## 1 ALASKA  On time LosAngeles     497
## 2 ALASKA  On time Phoenix        221
## 3 ALASKA  On time SanDiego       212
## 4 ALASKA  On time SanFrancisco   503
## 5 ALASKA  On time Seattle       1841
## 6 ALASKA  Delayed LosAngeles      62
unique(airline_long$Status)
## [1] "On time" "Delayed"
ggplot(city_summary,aes(x =city,y=DelayedRate, fill = Airline))+
  geom_col(position ="dodge")+ 
  labs(title = "delay Percentages by Airline and City", 
       x ="Destination",
       y ="Delay Percentage")+ theme_minimal()

conclusion

In conclusion, Alaska Airlines has a lower delay percentage than AmWest in each individual destination city. However, when all destinations are combined, AmWest appears to have a lower overall delay rate. This occurs because the airlines operate different numbers of flights to different destinations. The dataset demonstrates Simpson’s Paradox, where trends observed in separate groups can reverse when the groups are combined. This example highlights the importance of examining subgroup data rather than relying solely on overall summary statistics.