Nyc Flight assignment

Author

Andreas Sanches

NYC Flights Assignment

Install nycflights13 and tidyverse

Load tidyverse and nycflights13

Nycflights13 contain data about flights in all NYC airports in 2013

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.3     ✔ 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(nycflights13)

Load the dataset into your global environment

data(flights)

Rename “carrier”

Rename the column “carrier” that is in the 10th position in the dataset by “Airlines”.

colnames(flights)[10] = "Airlines"

See the different airlines

Use “unique” function to only show different airlines. I was curious to know how many were operating in NYC airports.

row <- unique(flights$Airlines)

Exclude “NA” values

Exclude “NA” values in Airlines and dep_delay columns.

flights_nona <- flights |>
 filter(!is.na(Airlines) & !is.na(dep_delay)) 

Group by airlines, count the number of flights and take the mean of dep_delay

Grouping by airline the number of flights give us an idea of the carriers who are more relevant in the NYC context. From that, choose the top-4 carriers, adding the condition of at least 35000 flights operated in 2013.

by_carrier <- flights_nona |> 
  group_by(Airlines) |>
  summarise(count = n(),
            delay = mean(dep_delay)) 
top_four <- filter(by_carrier, count > 35000 )

Rename the acronyms

Rename the acronyms of the carriers and change them to how they’re known.

top_four$Airlines[top_four$Airlines == "DL"]<-  "Delta"
top_four$Airlines[top_four$Airlines == "UA"]<- "United"
top_four$Airlines[top_four$Airlines == "EV"]<- "Expressjet"
top_four$Airlines[top_four$Airlines == "B6"]<- "JetBlue"

Create the Bar chart

It’s now time to put it all together. Using the “coord_flip” function you’ll flip the x and y axes. Labeling both axes and creating a legend is important to facilitate understanding and interpretation of the graph. Assigning colors through the “fill” function is also a feature that makes the visualization more interesting.

ggplot(top_four, aes(x = Airlines, y = delay, fill = Airlines)) +
  geom_bar(stat = "identity") +
  labs(x = "Airline names", y = "Delay in minutes", title =  "Average Departure Delays of The Four \n Biggest Airlines at NYC in 2013") +
 coord_flip()

I created a visualization that shows the average departure delay in minutes of the four biggest airlines at NYC airports. All 4 airlines in the graph had at least 35000 flights leaving from NYC, so the data is relevant since it covers many flights. I also made sure to exclude all the “NA” values either in the “airlines” column (formerly called carrier), or in the “dep_delay” column. One aspect that caught my attention is the high average delay in the “ExpressJet” flights compared to the other airlines. I then searched the internet to know better about this carrier (honestly I’ve never heard before) and figured they filed for bankruptcy in August last year. I investigated if there was something to do with their poor customer experience (since punctuality is a vital aspect) but they went out of business mainly because of “stifled growth, rising costs and lower revenue due to the pandemic.”