Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
── 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(knitr)library(scales)
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
# A tibble: 4 × 3
Airline Status Row_Total
<chr> <chr> <dbl>
1 ALASKA On Time 3274
2 ALASKA Delayed 501
3 AM WEST On Time 6438
4 AM WEST Delayed 787
kable( count_summary,caption ="Total Flight Counts by Airline and Status")
Total Flight Counts by Airline and Status
Airline
Status
Row_Total
ALASKA
On Time
3274
ALASKA
Delayed
501
AM WEST
On Time
6438
AM WEST
Delayed
787
# Transform Wide Data to Long Formatairline_long <- airline_filled %>%pivot_longer(cols =`Los Angeles`:Seattle,names_to ="City",values_to ="Flights" )print(airline_long)
# A tibble: 20 × 4
Airline Status City 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 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
# Now reshape the status variable for analysisairline_tidy <- airline_long %>%pivot_wider(names_from = Status,values_from = Flights ) %>%mutate(Total =`On Time`+ Delayed,Delay_Rate = Delayed / Total,On_Time_Rate =`On Time`/ Total ) %>%arrange( City, Airline )print(airline_tidy)
# A tibble: 10 × 7
Airline City `On Time` Delayed Total Delay_Rate On_Time_Rate
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 ALASKA Los Angeles 497 62 559 0.111 0.889
2 AM WEST Los Angeles 694 117 811 0.144 0.856
3 ALASKA Phoenix 221 12 233 0.0515 0.948
4 AM WEST Phoenix 4840 415 5255 0.0790 0.921
5 ALASKA San Diego 212 20 232 0.0862 0.914
6 AM WEST San Diego 383 65 448 0.145 0.855
7 ALASKA San Francisco 503 102 605 0.169 0.831
8 AM WEST San Francisco 320 129 449 0.287 0.713
9 ALASKA Seattle 1841 305 2146 0.142 0.858
10 AM WEST Seattle 201 61 262 0.233 0.767
# Formatted Tablekable( airline_tidy %>%mutate(Delay_Rate =percent( Delay_Rate,accuracy =0.1 ),On_Time_Rate =percent( On_Time_Rate,accuracy =0.1 ) ),caption ="Tidied Airline Data with Percentages")
# Alaska had 501 delayed flights out of 3,775 total flights, for an overall# delay rate of approximately 13.3%. AM West had 787 delayed flights out of# 7,225 total flights, for an overall delay rate of approximately 10.9%.# Based only on the overall percentages, AM West appears to have the lower# delay rate.# Delay Comparison Across Five Citiescity_summary <- airline_tidy %>%select( Airline, City,`On Time`, Delayed, Total, Delay_Rate ) %>%arrange( City, Airline )print(city_summary)
# A tibble: 10 × 6
Airline City `On Time` Delayed Total Delay_Rate
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 ALASKA Los Angeles 497 62 559 0.111
2 AM WEST Los Angeles 694 117 811 0.144
3 ALASKA Phoenix 221 12 233 0.0515
4 AM WEST Phoenix 4840 415 5255 0.0790
5 ALASKA San Diego 212 20 232 0.0862
6 AM WEST San Diego 383 65 448 0.145
7 ALASKA San Francisco 503 102 605 0.169
8 AM WEST San Francisco 320 129 449 0.287
9 ALASKA Seattle 1841 305 2146 0.142
10 AM WEST Seattle 201 61 262 0.233
# Tablekable( city_summary %>%mutate(Delay_Rate =percent( Delay_Rate,accuracy =0.1 ) ),caption ="Delay Percentages by Airline and City")
# A tibble: 5 × 3
City ALASKA `AM WEST`
<chr> <dbl> <dbl>
1 Los Angeles 0.111 0.144
2 Phoenix 0.0515 0.0790
3 San Diego 0.0862 0.145
4 San Francisco 0.169 0.287
5 Seattle 0.142 0.233
# Identify the better airline in each citybetter_by_city <- city_summary %>%group_by(City) %>%slice_min(order_by = Delay_Rate,n =1,with_ties =FALSE ) %>%ungroup() %>%select( City, Airline, Delay_Rate )print(better_by_city)
# A tibble: 5 × 3
City Airline Delay_Rate
<chr> <chr> <dbl>
1 Los Angeles ALASKA 0.111
2 Phoenix ALASKA 0.0515
3 San Diego ALASKA 0.0862
4 San Francisco ALASKA 0.169
5 Seattle ALASKA 0.142
# The city-by-city results show that Alaska had the lower delay percentage in# all five destinations:# Los Angeles: Alaska 11.1% vs. AM West 14.4%# Phoenix: Alaska 5.2% vs. AM West 7.9%# San Diego: Alaska 8.6% vs. AM West 14.5%# San Francisco: Alaska 16.9% vs. AM West 28.7%# Seattle: Alaska 14.2% vs. AM West 23.3%# Therefore, Alaska performed better within each individual destination.# Overall vs. City-by-City Discrepancybetter_overall <- overall_summary %>%slice_min(order_by = Delay_Rate,n =1,with_ties =FALSE ) %>%select( Airline, Delay_Rate )print(better_overall)
# A tibble: 1 × 2
Airline Delay_Rate
<chr> <dbl>
1 AM WEST 0.109
# The overall comparison and the city-by-city comparison lead to different# conclusions.# Overall, AM West has the lower delay rate, approximately 10.9%, compared# with Alaska's 13.3%. However, Alaska has the lower delay percentage in every# one of the five individual cities.# This discrepancy occurs because the two airlines do not operate the same# number of flights in each city. The overall delay percentage is weighted by# the number of flights.# AM West operates a very large number of flights in Phoenix, where its delay# rate is relatively low. These thousands of Phoenix flights strongly reduce# AM West's overall delay percentage.# As a result, AM West appears better overall even though Alaska performs# better in every individual city.# This type of reversal between subgroup results and combined results is known# as Simpson's Paradox.print(overall_summary)
# A tibble: 2 × 6
Airline On_Time Delayed Total Delay_Rate On_Time_Rate
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 ALASKA 3274 501 3775 0.133 0.867
2 AM WEST 6438 787 7225 0.109 0.891
print(city_comparison)
# A tibble: 5 × 3
City ALASKA `AM WEST`
<chr> <dbl> <dbl>
1 Los Angeles 0.111 0.144
2 Phoenix 0.0515 0.0790
3 San Diego 0.0862 0.145
4 San Francisco 0.169 0.287
5 Seattle 0.142 0.233
print(better_by_city)
# A tibble: 5 × 3
City Airline Delay_Rate
<chr> <chr> <dbl>
1 Los Angeles ALASKA 0.111
2 Phoenix ALASKA 0.0515
3 San Diego ALASKA 0.0862
4 San Francisco ALASKA 0.169
5 Seattle ALASKA 0.142
print(better_overall)
# A tibble: 1 × 2
Airline Delay_Rate
<chr> <dbl>
1 AM WEST 0.109