library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.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(ggplot2)
library(dplyr)
airlines = read.csv("~/Desktop/BANA 4137/airlines.csv", header = TRUE, stringsAsFactors = FALSE, na.strings = "")
airports = read.csv("~/Desktop/BANA 4137/airports.csv", header = TRUE, stringsAsFactors = FALSE, na.strings = "")
CVG_Flights = read.csv("~/Desktop/BANA 4137/CVG_Flights.csv", header = TRUE, stringsAsFactors = FALSE, na.strings = "")

QUESTION 1:

cat("CVG_Flights.csv - Variables:", length(names(CVG_Flights)), "\n")
## CVG_Flights.csv - Variables: 27
cat("CVG_Flights.csv - Observations:", nrow(CVG_Flights), "\n")
## CVG_Flights.csv - Observations: 7763
missing_values <- colSums(is.na(CVG_Flights))
if (any(missing_values > 0)) {
  cat("\nMissing Values in CVG_Flights.csv:\n")
  print(missing_values[missing_values > 0])
} else {
  cat("\nNo missing values in CVG_Flights.csv\n")
}
## 
## Missing Values in CVG_Flights.csv:
##      DEPARTURE_TIME     DEPARTURE_DELAY            TAXI_OUT          WHEELS_OFF 
##                 370                 370                 378                 378 
##        ELAPSED_TIME            AIR_TIME           WHEELS_ON             TAXI_IN 
##                 392                 392                 381                 381 
##        ARRIVAL_TIME       ARRIVAL_DELAY CANCELLATION_REASON    AIR_SYSTEM_DELAY 
##                 381                 392                7385                5814 
##      SECURITY_DELAY       AIRLINE_DELAY LATE_AIRCRAFT_DELAY       WEATHER_DELAY 
##                5814                5814                5814                5814

QUESTION 2:

summary_stats <- summary(CVG_Flights)
summary_stats
##  FLIGHT_DATE          AIRLINE          FLIGHT_NUMBER  ORIGIN_AIRPORT    
##  Length:7763        Length:7763        Min.   :  62   Length:7763       
##  Class :character   Class :character   1st Qu.:1908   Class :character  
##  Mode  :character   Mode  :character   Median :3246   Mode  :character  
##                                        Mean   :3194                     
##                                        3rd Qu.:4536                     
##                                        Max.   :6297                     
##                                                                         
##  DESTINATION_AIRPORT SCHEDULED_DEPARTURE DEPARTURE_TIME     DEPARTURE_DELAY 
##  Length:7763         Length:7763         Length:7763        Min.   :-24.00  
##  Class :character    Class :character    Class :character   1st Qu.: -5.00  
##  Mode  :character    Mode  :character    Mode  :character   Median : -1.00  
##                                                             Mean   : 12.35  
##                                                             3rd Qu.: 12.00  
##                                                             Max.   :708.00  
##                                                             NA's   :370     
##     TAXI_OUT       WHEELS_OFF        SCHEDULED_TIME   ELAPSED_TIME  
##  Min.   :  1.00   Length:7763        Min.   : 60.0   Min.   : 49.0  
##  1st Qu.: 12.00   Class :character   1st Qu.: 85.0   1st Qu.: 83.0  
##  Median : 15.00   Mode  :character   Median :126.0   Median :123.0  
##  Mean   : 18.52                      Mean   :128.1   Mean   :127.3  
##  3rd Qu.: 21.00                      3rd Qu.:156.0   3rd Qu.:152.0  
##  Max.   :122.00                      Max.   :313.0   Max.   :340.0  
##  NA's   :378                                         NA's   :392    
##     AIR_TIME        DISTANCE       WHEELS_ON            TAXI_IN       
##  Min.   : 36.0   Min.   : 229.0   Length:7763        Min.   :  1.000  
##  1st Qu.: 56.0   1st Qu.: 308.0   Class :character   1st Qu.:  5.000  
##  Median : 99.0   Median : 589.0   Mode  :character   Median :  6.000  
##  Mean   :100.4   Mean   : 675.9                      Mean   :  8.448  
##  3rd Qu.:125.0   3rd Qu.: 871.0                      3rd Qu.:  9.000  
##  Max.   :319.0   Max.   :2036.0                      Max.   :128.000  
##  NA's   :392                                         NA's   :381      
##  SCHEDULED_ARRIVAL  ARRIVAL_TIME       ARRIVAL_DELAY       DIVERTED       
##  Length:7763        Length:7763        Min.   :-56.00   Min.   :0.000000  
##  Class :character   Class :character   1st Qu.:-12.00   1st Qu.:0.000000  
##  Mode  :character   Mode  :character   Median : -2.00   Median :0.000000  
##                                        Mean   : 10.65   Mean   :0.001803  
##                                        3rd Qu.: 16.00   3rd Qu.:0.000000  
##                                        Max.   :716.00   Max.   :1.000000  
##                                        NA's   :392                        
##    CANCELLED       CANCELLATION_REASON AIR_SYSTEM_DELAY SECURITY_DELAY
##  Min.   :0.00000   Length:7763         Min.   :  0.00   Min.   :0.00  
##  1st Qu.:0.00000   Class :character    1st Qu.:  0.00   1st Qu.:0.00  
##  Median :0.00000   Mode  :character    Median : 10.00   Median :0.00  
##  Mean   :0.04869                       Mean   : 16.35   Mean   :0.01  
##  3rd Qu.:0.00000                       3rd Qu.: 23.00   3rd Qu.:0.00  
##  Max.   :1.00000                       Max.   :258.00   Max.   :9.00  
##                                        NA's   :5814     NA's   :5814  
##  AIRLINE_DELAY    LATE_AIRCRAFT_DELAY WEATHER_DELAY    
##  Min.   :  0.00   Min.   :  0.00      Min.   :  0.000  
##  1st Qu.:  0.00   1st Qu.:  0.00      1st Qu.:  0.000  
##  Median :  0.00   Median :  2.00      Median :  0.000  
##  Mean   : 17.06   Mean   : 23.24      Mean   :  3.873  
##  3rd Qu.: 18.00   3rd Qu.: 30.00      3rd Qu.:  0.000  
##  Max.   :435.00   Max.   :435.00      Max.   :638.000  
##  NA's   :5814     NA's   :5814        NA's   :5814
ggplot(CVG_Flights, aes(x = DEPARTURE_DELAY)) +
  geom_histogram(binwidth = 10, fill = "pink", color = "black") +
  labs(title = "Distribution of Departure Time",
       x = "Departure Time (minutes)",
       y = "Frequency")
## Warning: Removed 370 rows containing non-finite values (`stat_bin()`).

ggplot(CVG_Flights, aes(x = AIR_TIME)) +
  geom_histogram(binwidth = 10, fill = "darkred", color = "black") +
  labs(title = "Distribution of Air Time",
       x = "Air Time (minutes)",
       y = "Frequency")
## Warning: Removed 392 rows containing non-finite values (`stat_bin()`).

QUESTION 3:

ggplot(CVG_Flights, aes(x = AIR_TIME, y = DEPARTURE_DELAY)) +
  geom_point() +
  labs(title = "Association between Departure Time and Air Time",
       x = "Air Time (minutes)",
       y = "Departure Time (minutes)")
## Warning: Removed 392 rows containing missing values (`geom_point()`).

QUESTION 4:

ggplot(CVG_Flights, aes(x = AIRLINE, y = ORIGIN_AIRPORT)) +
  geom_point() +
  labs(title = "Association between Delay Time, Air Time, and Distance (conditional on Origin Airport)",
       x = "Air Time (minutes)",
       y = "Delay Time (minutes)",
       color = "Distance")

QUESTION 5:

merge_airlines <- left_join(CVG_Flights, airlines, by = c("AIRLINE" = "IATA_CODE"))
merge_origins <- left_join(merge_airlines, airports, by = c("ORIGIN_AIRPORT" = "IATA_CODE"))
flights_merged <- left_join(merge_origins, airports, by = c("DESTINATION_AIRPORT" = "IATA_CODE"))

QUESTION 6:

Question 1: Which airlines operate the most flights from CVG airport?

airline_counts <- flights_merged %>%
  group_by(AIRLINE.y) %>%
  summarise(Flight_Count = n()) %>%
  arrange(desc(Flight_Count))

ggplot(airline_counts, aes(x = reorder(AIRLINE.y, -Flight_Count), y = Flight_Count)) +
  geom_bar(stat = "identity", fill = "purple") +
  labs(title = "Number of Flights by Airline from CVG Airport",
       x = "Airline",
       y = "Flight Count")+
  theme(axis.text.x = element_text(angle = 30, hjust = 1))

Question 2: What are the most common destinations for flights departing from CVG airport?

destination_counts <- flights_merged %>%
  group_by(AIRPORT.y) %>%
  summarise(Flight_Count = n()) %>%
  arrange(desc(Flight_Count)) %>%
  top_n(10)
## Selecting by Flight_Count
ggplot(destination_counts, aes(x = reorder(AIRPORT.y, -Flight_Count), y = Flight_Count)) +
  geom_bar(stat = "identity", fill = "brown") +
  labs(title = "Top 10 Destinations from CVG Airport",
       x = "Destination Airport",
       y = "Flight Count") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

Question 3: What is the distribution of departure delays for flights departing from CVG airport?

flights_merged_filtered <- flights_merged[!is.na(flights_merged$DEPARTURE_DELAY), ]

ggplot(flights_merged_filtered, aes(x = DEPARTURE_DELAY)) +
  geom_histogram(fill = "hotpink", color = "black", bins = 30) +
  labs(title = "Distribution of Departure Delays from CVG Airport",
       x = "Departure Delay (minutes)",
       y = "Frequency")

Question 4: How does the number of flights departing from CVG airport vary over time?

flights_merged$FLIGHT_DATE <- as.Date(flights_merged$FLIGHT_DATE, format = "%m/%d/%Y")
flight_counts_by_date <- flights_merged %>%
  group_by(FLIGHT_DATE) %>%
  summarise(Flight_Count = n())

ggplot(flight_counts_by_date, aes(x = FLIGHT_DATE, y = Flight_Count)) +
  geom_line(color = "darkblue") +
  labs(title = "Number of Flights Departing from CVG Airport Over Time",
       x = "Date",
       y = "Flight Count")