Loading packages for the activity:
library(tidyverse)## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(lubridate)##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(knitr)
library(skimr)Importing the file:
michiganFlights <- readRDS("~/STA-418-518/fullMiFlights2021.rds")# Create tibbles in environment from list
list2env(michiganFlights, envir = .GlobalEnv)## <environment: R_GlobalEnv>
Exploratory Data Analysis
glimpse(weather)## Rows: 34,897
## Columns: 15
## $ origin <chr> "DTW", "DTW", "DTW", "DTW", "DTW", "DTW", "DTW", "DTW", "DT…
## $ year <int> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,…
## $ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ hour <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
## $ temp <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ dewp <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ humid <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ wind_dir <dbl> 210, 0, 0, 0, 0, 0, 50, 90, 0, 0, 70, 60, 50, 60, 80, 70, 1…
## $ wind_speed <dbl> 5.75390, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 2.301…
## $ wind_gust <dbl> 6.621473, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,…
## $ precip <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ pressure <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ visib <dbl> 8.0, 7.0, 6.0, 5.0, 4.0, 5.0, 5.0, 3.5, 3.0, 3.0, 3.0, 3.0,…
## $ time_hour <dttm> 2021-01-01 05:00:00, 2021-01-01 06:00:00, 2021-01-01 07:00…
skim(weather)| Name | weather |
| Number of rows | 34897 |
| Number of columns | 15 |
| _______________________ | |
| Column type frequency: | |
| character | 1 |
| numeric | 13 |
| POSIXct | 1 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| origin | 0 | 1 | 3 | 3 | 0 | 4 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| year | 0 | 1.00 | 2021.00 | 0.00 | 2021.00 | 2021.00 | 2021.00 | 2021.00 | 2021.00 | ▁▁▇▁▁ |
| month | 0 | 1.00 | 6.51 | 3.44 | 1.00 | 4.00 | 7.00 | 9.00 | 12.00 | ▇▆▆▆▇ |
| day | 0 | 1.00 | 15.67 | 8.77 | 1.00 | 8.00 | 16.00 | 23.00 | 31.00 | ▇▇▇▇▆ |
| hour | 0 | 1.00 | 11.50 | 6.92 | 0.00 | 6.00 | 12.00 | 18.00 | 23.00 | ▇▇▆▇▇ |
| temp | 34395 | 0.01 | 45.75 | 18.05 | 10.90 | 30.90 | 37.90 | 64.00 | 82.90 | ▂▇▃▃▃ |
| dewp | 34396 | 0.01 | 36.89 | 18.27 | 5.00 | 21.90 | 30.00 | 51.10 | 73.90 | ▅▇▂▅▃ |
| humid | 34397 | 0.01 | 72.73 | 15.76 | 31.52 | 61.59 | 72.07 | 87.06 | 100.00 | ▂▃▇▆▇ |
| wind_dir | 952 | 0.97 | 181.80 | 107.41 | 0.00 | 90.00 | 200.00 | 270.00 | 360.00 | ▇▃▇▇▆ |
| wind_speed | 465 | 0.99 | 8.09 | 5.36 | 0.00 | 4.60 | 8.06 | 11.51 | 36.82 | ▇▆▂▁▁ |
| wind_gust | 465 | 0.99 | 9.31 | 6.17 | 0.00 | 5.30 | 9.27 | 13.24 | 42.38 | ▇▆▂▁▁ |
| precip | 33636 | 0.04 | 0.01 | 0.03 | 0.00 | 0.00 | 0.01 | 0.01 | 0.44 | ▇▁▁▁▁ |
| pressure | 34629 | 0.01 | 1011.31 | 7.07 | 1000.10 | 1004.38 | 1011.15 | 1018.50 | 1024.00 | ▇▅▅▅▆ |
| visib | 99 | 1.00 | 8.80 | 2.29 | 0.06 | 9.00 | 10.00 | 10.00 | 10.00 | ▁▁▁▁▇ |
Variable type: POSIXct
| skim_variable | n_missing | complete_rate | min | max | median | n_unique |
|---|---|---|---|---|---|---|
| time_hour | 0 | 1 | 2021-01-01 05:00:00 | 2021-12-31 04:00:00 | 2021-07-02 03:00:00 | 8735 |
glimpse(flights)## Rows: 149,445
## Columns: 19
## $ year <int> 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2…
## $ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ dep_time <int> 536, 557, 558, 600, 606, 610, 611, 611, 624, 624, 627, …
## $ sched_dep_time <int> 539, 600, 600, 607, 600, 615, 615, 616, 630, 615, 600, …
## $ dep_delay <dbl> -3, -3, -2, -7, 6, -5, -4, -5, -6, 9, 27, -5, -5, -1, 0…
## $ arr_time <int> 738, 758, 700, 820, 905, 809, 809, 804, 711, 806, 808, …
## $ sched_arr_time <int> 825, 748, 730, 831, 920, 832, 822, 826, 723, 800, 834, …
## $ arr_delay <dbl> -47, 10, -30, -11, -15, -23, -13, -22, -12, 6, -26, -7,…
## $ carrier <chr> "AA", "DL", "NK", "OH", "NK", "OH", "DL", "YX", "DL", "…
## $ flight <int> 90, 174, 5, 512, 21, 507, 120, 491, 173, 284, 140, 157,…
## $ tailnum <chr> "N750UW", "N354NB", "N653NK", "N507AE", "N675NK", "N600…
## $ origin <chr> "DTW", "GRR", "DTW", "FNT", "DTW", "GRR", "DTW", "DTW",…
## $ dest <chr> "PHX", "ATL", "LAS", "CLT", "FLL", "CLT", "ATL", "CLT",…
## $ air_time <dbl> 227, 104, 220, 95, 162, 91, 95, 87, 28, 61, 207, 66, 15…
## $ distance <dbl> 1671, 640, 1749, 555, 1127, 583, 594, 500, 120, 409, 15…
## $ hour <dbl> 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 6, 7, 7, 7…
## $ minute <dbl> 39, 0, 0, 7, 0, 15, 15, 16, 30, 15, 0, 0, 0, 0, 0, 50, …
## $ time_hour <dttm> 2021-01-01 05:00:00, 2021-01-01 06:00:00, 2021-01-01 0…
skim(flights)| Name | flights |
| Number of rows | 149445 |
| Number of columns | 19 |
| _______________________ | |
| Column type frequency: | |
| character | 4 |
| numeric | 14 |
| POSIXct | 1 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| carrier | 0 | 1 | 2 | 2 | 0 | 15 | 0 |
| tailnum | 117 | 1 | 5 | 6 | 0 | 4136 | 0 |
| origin | 0 | 1 | 3 | 3 | 0 | 4 | 0 |
| dest | 0 | 1 | 3 | 3 | 0 | 114 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| year | 0 | 1.00 | 2021.00 | 0.00 | 2021 | 2021 | 2021 | 2021 | 2021 | ▁▁▇▁▁ |
| month | 0 | 1.00 | 6.77 | 3.35 | 1 | 4 | 7 | 10 | 12 | ▆▅▆▆▇ |
| day | 0 | 1.00 | 15.74 | 8.78 | 1 | 8 | 16 | 23 | 31 | ▇▇▇▇▆ |
| dep_time | 1382 | 0.99 | 1371.55 | 493.76 | 1 | 944 | 1353 | 1745 | 2400 | ▁▇▇▇▆ |
| sched_dep_time | 0 | 1.00 | 1369.27 | 486.06 | 500 | 930 | 1355 | 1737 | 2327 | ▇▇▆▇▆ |
| dep_delay | 1384 | 0.99 | 6.96 | 45.39 | -34 | -5 | -3 | 1 | 1948 | ▇▁▁▁▁ |
| arr_time | 1433 | 0.99 | 1464.41 | 517.39 | 1 | 1049 | 1455 | 1839 | 2400 | ▁▅▇▇▆ |
| sched_arr_time | 0 | 1.00 | 1481.47 | 507.65 | 1 | 1100 | 1504 | 1840 | 2359 | ▁▃▇▇▆ |
| arr_delay | 1715 | 0.99 | -0.22 | 47.48 | -79 | -17 | -9 | 1 | 1961 | ▇▁▁▁▁ |
| flight | 0 | 1.00 | 371.09 | 221.12 | 1 | 176 | 372 | 548 | 927 | ▇▇▇▆▁ |
| air_time | 1715 | 0.99 | 95.45 | 62.47 | 15 | 50 | 75 | 133 | 393 | ▇▃▁▁▁ |
| distance | 0 | 1.00 | 654.32 | 488.30 | 74 | 296 | 501 | 983 | 2986 | ▇▂▁▁▁ |
| hour | 0 | 1.00 | 13.42 | 4.83 | 5 | 9 | 13 | 17 | 23 | ▇▇▅▇▆ |
| minute | 0 | 1.00 | 27.52 | 18.71 | 0 | 10 | 29 | 45 | 59 | ▇▅▆▅▆ |
Variable type: POSIXct
| skim_variable | n_missing | complete_rate | min | max | median | n_unique |
|---|---|---|---|---|---|---|
| time_hour | 0 | 1 | 2021-01-01 05:00:00 | 2021-12-31 22:00:00 | 2021-07-12 21:00:00 | 6452 |
glimpse(airlines)## Rows: 15
## Columns: 2
## $ carrier <chr> "9E", "AA", "AS", "B6", "DL", "F9", "G4", "MQ", "NK", "OH", "O…
## $ name <chr> "Endeavor Air Inc.", "American Airlines Inc.", "Alaska Airline…
glimpse(planes)## Rows: 3,962
## Columns: 9
## $ tailnum <chr> "N101DQ", "N101DU", "N101HQ", "N102DN", "N102DU", "N102HQ…
## $ year <int> 2020, 2018, 2007, 2020, NA, 2007, 1998, NA, 2020, 2007, 2…
## $ type <chr> "Fixed wing multi engine", "Fixed wing multi engine", "Fi…
## $ manufacturer <chr> "AIRBUS", "C SERIES AIRCRAFT LTD PTNRSP", "EMBRAER-EMPRES…
## $ model <chr> "A321-211", "BD-500-1A10", "ERJ 170-200 LR", "A321-211", …
## $ engines <int> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ seats <int> 199, 133, 80, 199, 133, 80, 182, 133, 199, 80, 88, 182, 1…
## $ speed <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ engine <chr> "Turbo-fan", "Turbo-fan", "Turbo-fan", "Turbo-fan", "Turb…
flights and planes datasets are connected through tailnum
flights and airports are connected through origin and destination
flights and weather are connected through year, month, day, hour, flight, and origin
latitude, longitude. origin, and destination
we need to combine airports and flights
Joining data.
# Combine the airlines and flights data frames with left_join() to create a new data set called flightsCarriers.
flightsCarriers <- flights %>% left_join(airlines, by = c("carrier" = "carrier"))
#Calculate the average flight distance for each carrier using the full name of the carriers. Who had the longest flights on average? Who had the shortest?
flightsCarriers %>% group_by(name) %>%
summarize(AvgDistance = mean(distance, na.rn = TRUE)) %>%
arrange(AvgDistance) %>% knitr::kable()| name | AvgDistance |
|---|---|
| Endeavor Air Inc. | 327.8897 |
| Envoy Air | 362.2711 |
| Republic Airline | 384.4040 |
| SkyWest Airlines Inc. | 428.0904 |
| PSA Airlines Inc. | 510.3483 |
| JetBlue Airways | 583.6000 |
| Southwest Airlines Co. | 600.4704 |
| United Air Lines Inc. | 678.6150 |
| Mesa Airlines Inc. | 763.6516 |
| American Airlines Inc. | 912.3228 |
| Delta Air Lines Inc. | 959.6660 |
| Spirit Air Lines | 1113.1827 |
| Allegiant Air | 1117.4148 |
| Frontier Airlines Inc. | 1177.0890 |
| Alaska Airlines Inc. | 1927.0000 |
# Combine the weather and flights data frames with left_join() to create a new data set called weatherFlights. How many rows does weatherFlights have?
weatherFlights <- weather %>% left_join(flights,
by = c("origin" = "origin",
"year" = "year",
"month" = "month",
"day" = "day",
"hour" = "hour"))
weatherFlights %>% nrow()## [1] 168159
#Combine the flights and weather data frames with left_join() to create a new data set called flightsWeather. How many rows does flightsWeather have?
flightsWeather <- flights %>% left_join(weather, by = c("origin" = "origin",
"year" = "year",
"month" = "month",
"day" = "day",
"hour" = "hour"))
flightsWeather %>% nrow()## [1] 149445
#Combine the weather and flights data frames with full_join() to create a new data set called weatherFlightsFull. How many rows does weatherFlightsFull have?
weatherFlightsFull <- flights %>% full_join(weather, by = c("origin" = "origin",
"year" = "year",
"month" = "month",
"day" = "day",
"hour" = "hour"))
weatherFlightsFull %>% nrow()## [1] 168504
flightsWeather %>% pull(wind_speed) %>% is.na() %>% sum()## [1] 1526
Inner Joins
#Combine the weather and flights data frames with inner_join() to create a new data set called innerWeatherFlights. How many rows does innerWeatherFlights have?
innerWeatherFlights <- weather %>% inner_join(flights, by = c("origin" = "origin",
"year" = "year",
"month" = "month",
"day" = "day",
"hour" = "hour"))
innerWeatherFlights %>% nrow()## [1] 149100
nrow(weatherFlights)## [1] 168159
nrow(weatherFlights) - nrow(innerWeatherFlights)## [1] 19059
There were about 19,059 weather recorded without any flights occurring.