Create the data Read the database into a data frame Create a .csv file containing the information Using “wide” data structure Or, optionally, an SQL DB

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(RSQLite)
library(ggplot2)
library(tidyr)

#Creating a database using SQLite again, as it's well suited for these small modular tasks.
con = dbConnect(SQLite(), "Flights.db")
dbSendQuery(con, "DROP TABLE IF EXISTS Flight_Info")
dbSendQuery(con, "CREATE TABLE Flight_Info (Airport TEXT, IsDelayed INTEGER, Los_Angeles INTEGER, Phoenix INTEGER, San_Diego INTEGER, San_Francisco INTEGER, Seattle INTEGER)")

dbSendQuery(con, "INSERT INTO Flight_Info VALUES ('ALASKA', 0, 497, 22, 212, 503, 1841)")
dbSendQuery(con, "INSERT INTO Flight_Info VALUES ('ALASKA', 1, 62, 12, 20, 102, 305)")
dbSendQuery(con, "INSERT INTO Flight_Info VALUES ('AM WEST', 0, 694, 4840, 383, 320, 201)")
dbSendQuery(con, "INSERT INTO Flight_Info VALUES ('AM WEST', 1, 117, 415, 65, 129, 61)")

Read the information from your .CSV file into R Use tidyr as needed to tidy data. Use dplyr as needed to transform data.

flight_info = dbReadTable(con, 'flight_info')
## Warning: Closing open result set, pending rows
#Make data "tall" using gather and spread
tall_flights = flight_info %>% gather(Destination, Count, Los_Angeles:Seattle) %>% spread(IsDelayed, Count)

#Fix the column names
colnames(tall_flights)[3] = "On_Time"
colnames(tall_flights)[4] = "Delayed"

#Add percentages
tall_flights$On_Time_Percentage = round((tall_flights$On_Time / (tall_flights$On_Time + tall_flights$Delayed)) * 100, 2)
tall_flights
##    Airport   Destination On_Time Delayed On_Time_Percentage
## 1   ALASKA   Los_Angeles     497      62              88.91
## 2   ALASKA       Phoenix      22      12              64.71
## 3   ALASKA     San_Diego     212      20              91.38
## 4   ALASKA San_Francisco     503     102              83.14
## 5   ALASKA       Seattle    1841     305              85.79
## 6  AM WEST   Los_Angeles     694     117              85.57
## 7  AM WEST       Phoenix    4840     415              92.10
## 8  AM WEST     San_Diego     383      65              85.49
## 9  AM WEST San_Francisco     320     129              71.27
## 10 AM WEST       Seattle     201      61              76.72

Compare the per-city on-time performance for both airlines

# dodged barplot with multiple groups
ggplot(data=tall_flights, aes(x=Destination, y=On_Time_Percentage, fill=Airport)) +
  geom_bar(stat="identity", position=position_dodge())

Compare the overall on-time performance for both airlines

#Create data frames for total on time percentages
grouped_flights = tall_flights %>% group_by(Airport) %>% summarise_at(2:3, funs(sum))

#Add percentages
grouped_flights$On_Time_Percentage = round((grouped_flights$On_Time / (grouped_flights$On_Time + grouped_flights$Delayed)) * 100, 2)
grouped_flights
## # A tibble: 2 x 4
##   Airport On_Time Delayed On_Time_Percentage
##   <chr>     <int>   <int>              <dbl>
## 1 ALASKA     3075     501               86.0
## 2 AM WEST    6438     787               89.1
#Standard bar plot
ggplot(data=grouped_flights, aes(x=Airport, y=On_Time_Percentage, fill=Airport)) +
  geom_bar(stat="identity")

Explain the apparent discrepancy between the per-city and overall performances Provide an interesting paradoxical conclusion Provide a conclusion (including any findings and recommendations)

Conclusions:

Based on the data, AM West has a slightly better on time percentage across all airlines than Alaska does, despite having worse performance in every airport except Phoenix. The reason this is, is that the discrepancy in Phoenix is SO large, that it allows AM west to claim the better avg time.

Looking at the raw numbers, this is likely an anomaly being caused by a lack of data, as Alaska has barely any flights in Phoenix. I think more data on Alaska flights to Phoenix are needed to make a fair comparison, and if none are available, it might be prudent to attempt to impute data from previous years to fill in the gaps here.