Skip to content

A foremost aviation industry player with a significant presence in New York City has launched an in-depth data analysis project focused on identifying trends in flight durations in air travel. This initiative aims to delve into a wealth of data related to flight schedules and operational patterns, with the objective of optimizing flight times and enhancing the overall travel experience for passengers. As the head data analyst, you have access to rich datasets, sourced from the 'nycflights2022' collection produced by the ModernDive team. These datasets include records of flights departing from major New York City airports, including JFK (John F. Kennedy International Airport), LGA (LaGuardia Airport), and EWR (Newark Liberty International Airport), during the second half of 2022. They offer a comprehensive view of flight operations, covering various aspects such as departure and arrival times, flight paths, and airline specifics:

  • flights2022-h2.csv contains information about each flight including
VariableDescription
carrierAirline carrier code
originOrigin airport (IATA code)
destDestination airport (IATA code)
air_timeDuration of the flight in air, in minutes
  • airlines.csv contains information about each airline:
VariableDescription
carrierAirline carrier code
nameFull name of the airline
  • airports.csv provides details of airports:
VariableDescription
faaFAA code of the airport
nameFull name of the airport
# Import required packages
library(dplyr)
library(readr)

# Load the data
flights <- read_csv("flights2022-h2.csv")
airlines <- read_csv("airlines.csv")
airports <- read_csv("airports.csv")

# Start your code here!

glimpse(flights)
glimpse(airlines)
glimpse(airports)

df_nyc<- flights %>% filter(origin=="NYC")

head(df_nyc)
complex_join <- flights %>% 
				left_join(airlines, by="carrier")	%>%
				rename(airline_name = name) %>% 
				left_join(airports, by=c("dest"="faa"),suffix = c("_flight","_airport")) %>%
				rename(airport_name = name)
head(complex_join)
frequent <- complex_join %>% group_by(airline_name,airport_name,) %>% 
					summarise(cnt=n(),.groups = "drop")%>% 
					arrange(desc(cnt)) %>%
					slice_max(cnt,n=1)
					

frequent

longest <- complex_join %>%
  group_by(airport_name) %>%
  reframe(
    flight_duration = air_time / 60,  # Convert air_time to hours
    avg_flight = mean(flight_duration, na.rm = TRUE)  # Calculate the average flight duration
  ) %>%
  arrange(desc(avg_flight))  # Arrange by average flight duration in descending order


						
longest
transformed_data <- complex_join %>%
  mutate(flight_duration = air_time / 60)
a <- transformed_data %>%
  group_by(airline_name, airport_name) %>%
  summarize(avg_flight_duration = mean(flight_duration, na.rm = TRUE),
            count = n(),.groups="drop") 
 
a
# Import required packages
library(dplyr)
library(readr)

# Load the data
flights <- read_csv("flights2022-h2.csv")
airlines <- read_csv("airlines.csv")
airports <- read_csv("airports.csv")

# Join the flights, airlines, and airports data frames together
complex_join <- flights %>%
  left_join(airlines, by = "carrier") %>%
  rename(airline_name = name) %>% 
  left_join(airports, by = c("dest" = "faa")) %>% 
  rename(airport_name = name)

# Find flight duration in hours
transformed_data <- complex_join %>%
  mutate(flight_duration = air_time / 60)

# Determine the average flight duration and number of flights for each airline and airport combination
analysis_result <- transformed_data %>%
  group_by(airline_name, airport_name) %>%
  summarize(avg_flight_duration = mean(flight_duration, na.rm = TRUE),
            count = n()) %>%
  ungroup()

# From which airline and to which city do the most flights from NYC go to?
frequent <- analysis_result %>% arrange(desc(count)) %>% head(1)

# Which airline and to which airport has the longest average flight duration (in hours) from NYC?
longest <- analysis_result %>% arrange(desc(avg_flight_duration)) %>% head(1)

# What was the least common destination airport departing from JFK?
transformed_data %>% 
  filter(origin == "JFK") %>% 
  group_by(airport_name) %>% 
  summarize(count = n()) %>% 
  arrange(count)

least <- "Eagle County Regional Airport"