In this lab we explore flights, specifically a random sample of domestic flights that departed from the three major New York City airports in 2013.

We will generate simple graphical and numerical summaries of data on these flights and explore delay times. You can find the instructions for this lab here

# 1. load the library "tidyverse"
library(tidyverse)

# 2. use the read_csv file to read the dataset
nycflights <- read_csv("data/nycflights.csv")

Exercise 1

Question: Experiment with different binwidths to adjust your histogram in a way that will show its important features. You may also want to try using the + scale_x_log10(). What features are revealed now that were obscured in the original histogram?

# Write your code to create a histogram of delays
ggplot(data = nycflights, 
       aes(x = dep_delay)) +
  geom_histogram(binwidth = 0.05) +
    scale_x_log10()
## Warning in self$trans$transform(x): NaNs produced
## Warning: Transformation introduced infinite values in continuous x-axis
## Warning: Removed 19936 rows containing non-finite values (`stat_bin()`).

Answer: From 1 to 10 it goes down almost linearly, then it has a bump.

Exercise 2

Question: Create a new data frame that includes flights headed to SFO in February, and save this data frame as sfo_feb_flights. How many flights meet these criteria?

# Insert code for Exercise 2 here
sfo_feb_flights <- nycflights %>%
  filter(dest == "SFO", month == 2)

Answer: 68 flights meet the criteria.

Exercise 3

Question: Describe the distribution of the arrival delays of flights headed to SFO in February, using an appropriate histogram and summary statistics.

# Insert code for Exercise 3 here
sfo_feb_flights |>
  summarise(mean_ad = mean(arr_delay),
            median_ad = median(arr_delay),
            iqr_ad = IQR(arr_delay),
            n = n())
ggplot(data = sfo_feb_flights, 
       aes(x = arr_delay)) +
  geom_histogram(binwidth = 2.5)

Answer: The distribution of arrival delays of flights headed to SFO is skewed to the right, meaning there are some outliers where the arrival delay was higher.

Exercise 4

Question: Calculate the median and interquartile range for arr_delays of flights in in the sfo_feb_flights data frame, grouped by carrier. Which carrier has the most variable arrival delays?

# Insert code for the Exercise here
sfo_feb_flights |>
  group_by(carrier) |>
  summarise(
    median_ad = median(arr_delay),
    iqr_ad = IQR(arr_delay),
    n_flights = n())

Answer: DL and UA both have equal IQR values for arrival delays.

Exercise 5

Question: Create a list of origin airports and their rate of on-time-departure. Then visualize the distribution of on-time-departure rate across the three airports using a segmented bar plot (see below). If you could select an airport based on on time departure percentage, which NYC airport would you choose to fly out of? Hint: For the segmented bar plot, will need to map the aesthetic arguments as follows: x = origin, fill = dep_type and a geom_bar() layer. Create three plots, one with geom_bar() layer, one with geom_bar(position = "fill") and the third with geom_bar(position = "dodge"). Explain the difference between the three results.

# Insert code for the Exercise here
nycflights <- nycflights |>
  mutate(dep_type = ifelse(dep_delay < 5, "on time", "delayed"))

nycflights |>
  ggplot(aes(x = origin, fill = dep_type)) +
  geom_bar()

nycflights |>
  ggplot(aes(x = origin, fill = dep_type)) +
  geom_bar(position = "fill")

nycflights |>
  ggplot(aes(x = origin, fill = dep_type)) +
  geom_bar(position = "dodge") +
  labs(fill = "departure_type", y = "proportion")

We would choose LGA because the proportion of delayed as opposed to on time flights is the smallest.

Exercise 6

Question: Mutate the data frame so that it includes a new variable that contains the average speed, avg_speed traveled by the plane for each flight (in mph, or if you are brave, in km/h). Now make a scatter plot of distance vs. avg_speed. Think carefully which of the two variables is the predictor (on the x-axis) and which is the outcome variable (on the y-axis) and explain why you made this choice. Describe the relationship between average speed and distance.

# Insert code for the Exercise here
nycflights <- nycflights |>
  mutate(air_time_hours = air_time / 60) |>
  mutate(speed = distance / air_time_hours)

nycflights |>
  ggplot(aes(x = distance, y = speed)) +
geom_point()

Answer: Generally speaking, the longer the distance the higher the average speed. Of course, speed is limited, so it seems to cap out at around 400 to 500 miles per hour.

Exercise 7

Question: Replicate the following plot and determine what is the cutoff point for the latest departure delay where you can still have a chance to arrive at your destination on time.

# Insert code for the Exercise here
nycflights |>
  filter(carrier %in% c("UA", "AA", "DL")) |>
  ggplot(aes(x=dep_delay, y= arr_delay, color=carrier)) +
  geom_point()

Answer: The cut-off point is the point in the very top right of the graph, which is about 825 minutes.