Class 606, Lab 2

Author

Troy Tournat

Published

September 11, 2026

Location of lab: https://htmlpreview.github.io/?https://github.com/jbryer/DATA606/blob/master/inst/labs/Lab2/Lab2_intro_to_data.html

Citations:

Setting up and loading data:

#load packages 
library(tidyverse)
library(openintro)

#load data 
data('nycflights', package='openintro')

#see data
glimpse(nycflights)
Rows: 32,735
Columns: 16
$ year      <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, …
$ month     <int> 6, 5, 12, 5, 7, 1, 12, 8, 9, 4, 6, 11, 4, 3, 10, 1, 2, 8, 10…
$ day       <int> 30, 7, 8, 14, 21, 1, 9, 13, 26, 30, 17, 22, 26, 25, 21, 23, …
$ dep_time  <int> 940, 1657, 859, 1841, 1102, 1817, 1259, 1920, 725, 1323, 940…
$ dep_delay <dbl> 15, -3, -1, -4, -3, -3, 14, 85, -10, 62, 5, 5, -2, 115, -4, …
$ arr_time  <int> 1216, 2104, 1238, 2122, 1230, 2008, 1617, 2032, 1027, 1549, …
$ arr_delay <dbl> -4, 10, 11, -34, -8, 3, 22, 71, -8, 60, -4, -2, 22, 91, -6, …
$ carrier   <chr> "VX", "DL", "DL", "DL", "9E", "AA", "WN", "B6", "AA", "EV", …
$ tailnum   <chr> "N626VA", "N3760C", "N712TW", "N914DL", "N823AY", "N3AXAA", …
$ flight    <int> 407, 329, 422, 2391, 3652, 353, 1428, 1407, 2279, 4162, 20, …
$ origin    <chr> "JFK", "JFK", "JFK", "JFK", "LGA", "LGA", "EWR", "JFK", "LGA…
$ dest      <chr> "LAX", "SJU", "LAX", "TPA", "ORF", "ORD", "HOU", "IAD", "MIA…
$ air_time  <dbl> 313, 216, 376, 135, 50, 138, 240, 48, 148, 110, 50, 161, 87,…
$ distance  <dbl> 2475, 1598, 2475, 1005, 296, 733, 1411, 228, 1096, 820, 264,…
$ hour      <dbl> 9, 16, 8, 18, 11, 18, 12, 19, 7, 13, 9, 13, 8, 20, 12, 20, 6…
$ minute    <dbl> 40, 57, 59, 41, 2, 17, 59, 20, 25, 23, 40, 20, 9, 54, 17, 24…
#explore dataset 
names(nycflights)
 [1] "year"      "month"     "day"       "dep_time"  "dep_delay" "arr_time" 
 [7] "arr_delay" "carrier"   "tailnum"   "flight"    "origin"    "dest"     
[13] "air_time"  "distance"  "hour"      "minute"   

#

#

Exercise 1: Look carefully at these three histograms. How do they compare? Are features revealed in one that are obscured in another?

ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

## adding width feature
ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 15) 

ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 150)  ## adding histogram bar width 

Answer: Out of all three, the second histogram (binwidth of 15) shows the most information because you can see on the x-axis a negative departure delay.

The third histogram obscures much of the departure delay information because of the 150 binwidth.

If we are trying to understand the full distribution of departure delays though the three histograms are not visually displaying enough information.

#

#

Exercise 2: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?

sfo_feb_flights <- nycflights %>%
  filter(dest == "SFO", month == 2)

Answer: 68 flights

#

#

Exercise 3:Describe the distribution of the arrival delays of these flights using a histogram and appropriate summary statistics. Hint: The summary statistics you use should depend on the shape of the distribution.

sfo_feb_flights <- nycflights %>%
  filter(dest == "SFO", month == 2)

#figure out the distribution in order to visualize
sfo_feb_flights %>%
  summarise(
    n         = n(),
    min_dd = min(dep_delay),
    mean_dd   = mean(dep_delay), 
    median_dd = median(dep_delay), 
    mode_dd =  mode(dep_delay), 
    max_dd = max(dep_delay), 
    iqr_dd = IQR(dep_delay)
  )
# A tibble: 1 × 7
      n min_dd mean_dd median_dd mode_dd max_dd iqr_dd
  <int>  <dbl>   <dbl>     <dbl> <chr>    <dbl>  <dbl>
1    68    -10    10.5        -2 numeric    209     14
#histogram
ggplot(data = sfo_feb_flights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 1) 

Answer: Looking at the summary statistics we see the distribution of the 68 flights from February with a destination to SFO has delay range from -10 (10 min ahead of schedule) to 209 (approximately 3.5 hours delayed). The the median is -2 (2 minutes ahead of schedule). The histogram shows us that the delays past 50 minutes could be outliers.

#

#

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

sfo_feb_flights %>%
  group_by(carrier) %>%
  summarise(median_dd = median(arr_delay), iqr_dd = IQR(arr_delay), n_flights = n())
# A tibble: 5 × 4
  carrier median_dd iqr_dd n_flights
  <chr>       <dbl>  <dbl>     <int>
1 AA            5     17.5        10
2 B6          -10.5   12.2         6
3 DL          -15     22          19
4 UA          -10     22          21
5 VX          -22.5   21.2        12

Answer: The airlines that have the most variable arrival delays is either DL or UA with an IQR spread of 22 minutes into SFO in Feb.

#

#

Exercise 5: Suppose you really dislike departure delays and you want to schedule your travel in a month that minimizes your potential departure delay leaving NYC. One option is to choose the month with the lowest mean departure delay. Another option is to choose the month with the lowest median departure delay. What are the pros and cons of these two choices?

nycflights %>%
  group_by(month) %>%
  summarise(mean_dd = mean(dep_delay), median_dd = median(dep_delay)) %>%
  arrange(month)
# A tibble: 12 × 3
   month mean_dd median_dd
   <int>   <dbl>     <dbl>
 1     1   10.2         -2
 2     2   10.7         -2
 3     3   13.5         -1
 4     4   14.6         -2
 5     5   13.3         -1
 6     6   20.4          0
 7     7   20.8          0
 8     8   12.6         -1
 9     9    6.87        -3
10    10    5.88        -3
11    11    6.10        -2
12    12   17.4          1
#see if should use mean or median (normal or skewed)
#overall skewed 
#look by month, yes skewed 
nycflights %>%
  #filter(month == 1)%>%
  summarise(
    n         = n(),
    min_dd = min(dep_delay),
    mean_dd   = mean(dep_delay), 
    median_dd = median(dep_delay), 
    #mode_dd =  mode(dep_delay), 
    max_dd = max(dep_delay), 
    iqr_dd = IQR(dep_delay)
  )
# A tibble: 1 × 6
      n min_dd mean_dd median_dd max_dd iqr_dd
  <int>  <dbl>   <dbl>     <dbl>  <dbl>  <dbl>
1 32735    -21    12.7        -2   1301     16
#histogram
ggplot(data = nycflights, aes(x = dep_delay)) +
  geom_histogram(binwidth = 1) 

Answer: It would be better to go with the median because the dataset is skewed with outliers. With this in mind, out of all the months either September or October would be the best months to travel because the median delay is 3 minutes ahead of schedule. On the flipside, we can’t determine from the median which month is better because of the way the median is calculated. If it matters exactly which month, we might consider using the mean because we will get a specific number that takes into account all numbers in the dataset. Looking at the mean, the best month is October with an average delay of 5.88 minutes.

#

#

Exercise 3: If you were selecting an airport simply based on on time departure percentage, which NYC airport would you choose to fly out of?

nycflights <- nycflights %>%
  mutate(dep_type = ifelse(dep_delay < 5, "on time", "delayed"))

nycflights %>%
  group_by(origin) %>%
  summarise(ot_dep_rate = sum(dep_type == "on time") / n()) %>%
  arrange(desc(ot_dep_rate))
# A tibble: 3 × 2
  origin ot_dep_rate
  <chr>        <dbl>
1 LGA          0.728
2 JFK          0.694
3 EWR          0.637
ggplot(data = nycflights, aes(x = origin, fill = dep_type)) +
  geom_bar()

Answer: I would choose LGA because out of the three airports it has the highest percentage of on time departures.

#

#

Exercise 7: 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). Hint: Average speed can be calculated as distance divided by number of hours of travel, and note that air_time is given in minutes.

head(nycflights)
# A tibble: 6 × 17
   year month   day dep_time dep_delay arr_time arr_delay carrier tailnum flight
  <int> <int> <int>    <int>     <dbl>    <int>     <dbl> <chr>   <chr>    <int>
1  2013     6    30      940        15     1216        -4 VX      N626VA     407
2  2013     5     7     1657        -3     2104        10 DL      N3760C     329
3  2013    12     8      859        -1     1238        11 DL      N712TW     422
4  2013     5    14     1841        -4     2122       -34 DL      N914DL    2391
5  2013     7    21     1102        -3     1230        -8 9E      N823AY    3652
6  2013     1     1     1817        -3     2008         3 AA      N3AXAA     353
# ℹ 7 more variables: origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
#   hour <dbl>, minute <dbl>, dep_type <chr>
#average speed = distance / hours 

nycflights_speed <- nycflights%>% 
  mutate( 
    air_time_hr = air_time/60, 
    avg_speed = distance/air_time_hr
    )

head(nycflights_speed$avg_speed)
[1] 474.4409 443.8889 394.9468 446.6667 355.2000 318.6957

Exercise 8:Make a scatterplot of avg_speed vs. distance. Describe the relationship between average speed and distance. Hint: Use geom_point().

ggplot(data = nycflights_speed, aes(x = distance, y= avg_speed)) +
  geom_point()

Answer: We see a logarithmic curve where the average speed jumps up quickly at a short distance, increases as the distance increases but this flattens after 1000 miles to over 400 mph.
#

#

Exercise 9: Replicate the following plot. Hint: The data frame plotted only contains flights from American Airlines, Delta Airlines, and United Airlines, and the points are colored by carrier. Once you replicate the plot, determine (roughly) what the cutoff point is for departure delays where you can still expect to get to your destination on time.

nycflights_subcarrier <- nycflights %>% 
  filter(carrier %in% c("AA","DL","UA"))

ggplot(data = nycflights_subcarrier, aes(x = dep_delay, y= arr_delay, color = carrier),
  col = ) + 
  geom_point()

Answer: Roughly, if your departure delay is up to 75 minutes late you could still arrive on time.

#

#