library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ 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 data.

library(readr)
miFlights <- read_csv("Activity 7/miFlights2019-2021.csv")
## Rows: 463818 Columns: 37
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (9): carrier, tailnum, origin, dest, carrier_name, plane_type, plane_m...
## dbl  (27): year, month, day, dep_time, sched_dep_time, dep_delay, arr_time, ...
## dttm  (1): time_hour
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Exploratory Data Analysis.

skim(miFlights)
Data summary
Name miFlights
Number of rows 463818
Number of columns 37
_______________________
Column type frequency:
character 9
numeric 27
POSIXct 1
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
carrier 0 1.00 2 2 0 16 0
tailnum 2189 1.00 3 6 0 5250 0
origin 0 1.00 3 3 0 4 0
dest 0 1.00 3 3 0 130 0
carrier_name 0 1.00 9 34 0 16 0
plane_type 11140 0.98 23 23 0 1 0
plane_manufacturer 11140 0.98 6 29 0 16 0
plane_model 11140 0.98 5 15 0 93 0
plane_engine 11140 0.98 9 9 0 2 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
year 0 1.00 2019.92 0.85 2019.00 2019.00 2020.00 2021.00 2021.00 ▇▁▆▁▆
month 0 1.00 6.53 3.48 1.00 3.00 7.00 10.00 12.00 ▇▅▅▅▇
day 0 1.00 15.74 8.76 1.00 8.00 16.00 23.00 31.00 ▇▇▇▇▆
dep_time 9060 0.98 1372.26 490.52 1.00 950.00 1355.00 1754.00 2400.00 ▁▇▇▇▆
sched_dep_time 0 1.00 1368.40 481.75 49.00 948.00 1355.00 1750.00 2336.00 ▁▇▇▇▆
dep_delay 9063 0.98 7.11 44.97 -54.00 -5.00 -3.00 0.00 2672.00 ▇▁▁▁▁
arr_time 9324 0.98 1481.01 506.74 1.00 1053.00 1502.00 1905.00 2400.00 ▁▅▇▇▆
sched_arr_time 0 1.00 1496.79 495.16 1.00 1103.00 1510.00 1910.00 2359.00 ▁▃▇▇▇
arr_delay 10239 0.98 0.16 47.21 -85.00 -17.00 -9.00 2.00 2649.00 ▇▁▁▁▁
flight 0 1.00 413.37 269.57 1.00 189.00 387.00 600.00 1322.00 ▇▇▆▂▁
air_time 10239 0.98 94.59 63.12 15.00 50.00 74.00 130.00 581.00 ▇▂▁▁▁
distance 0 1.00 641.00 488.23 74.00 296.00 500.00 957.00 4475.00 ▇▂▁▁▁
hour 0 1.00 13.41 4.79 0.00 9.00 13.00 17.00 23.00 ▁▇▇▇▆
minute 0 1.00 27.49 17.94 0.00 11.00 27.00 44.00 59.00 ▇▆▇▆▆
temp 441760 0.05 42.16 15.40 -4.00 32.00 37.90 48.90 90.00 ▁▆▇▂▁
dewp 441762 0.05 31.91 13.42 -9.00 23.00 28.90 39.90 75.90 ▁▆▇▃▁
humid 441773 0.05 68.87 15.10 25.87 57.93 71.82 80.66 100.00 ▁▃▅▇▃
wind_dir 9205 0.98 181.02 109.46 0.00 80.00 200.00 270.00 360.00 ▇▃▆▇▆
wind_speed 4367 0.99 8.59 5.64 0.00 4.60 8.06 11.51 42.58 ▇▆▁▁▁
wind_gust 4367 0.99 9.88 6.50 0.00 5.30 9.27 13.24 49.00 ▇▆▁▁▁
precip 430846 0.07 0.01 0.02 0.00 0.00 0.00 0.01 0.44 ▇▁▁▁▁
pressure 447131 0.04 1018.83 7.60 990.40 1014.10 1019.00 1023.40 1038.50 ▁▂▇▇▂
visib 1934 1.00 8.18 2.56 0.06 7.00 10.00 10.00 10.00 ▁▁▁▂▇
plane_year 21647 0.95 2008.12 7.15 1987.00 2003.00 2007.00 2015.00 2021.00 ▁▂▇▃▅
plane_engines 11140 0.98 2.00 0.02 2.00 2.00 2.00 2.00 3.00 ▇▁▁▁▁
plane_seats 11140 0.98 127.86 66.68 20.00 80.00 95.00 182.00 451.00 ▇▃▂▁▁
plane_speed 11140 0.98 0.01 1.72 0.00 0.00 0.00 0.00 438.00 ▇▁▁▁▁

Variable type: POSIXct

skim_variable n_missing complete_rate min max median n_unique
time_hour 0 1 2019-01-01 05:00:00 2021-12-31 22:00:00 2020-03-26 06:00:00 19059
glimpse(miFlights)
## Rows: 463,818
## Columns: 37
## $ year               <dbl> 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 201…
## $ month              <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ day                <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ dep_time           <dbl> 55, 455, 506, 531, 534, 550, 555, 555, 555, 600, 60…
## $ sched_dep_time     <dbl> 2115, 500, 511, 535, 545, 600, 600, 555, 600, 600, …
## $ dep_delay          <dbl> 220, -5, -5, -4, -11, -10, -5, 0, -5, 0, -2, 3, 4, …
## $ arr_time           <dbl> 426, 830, 710, 647, 750, 712, 822, 709, 755, 559, 9…
## $ sched_arr_time     <dbl> 2323, 834, 730, 710, 742, 748, 834, 715, 817, 615, …
## $ arr_delay          <dbl> 303, -4, -20, -23, 8, -36, -12, -6, -22, -16, 3, -7…
## $ carrier            <chr> "OH", "YX", "AA", "WN", "B6", "YX", "OO", "WN", "DL…
## $ flight             <dbl> 1019, 954, 185, 203, 310, 790, 803, 295, 348, 218, …
## $ tailnum            <chr> "N567NN", "N433YX", "N853NN", "N227WN", "N203JB", "…
## $ origin             <chr> "DTW", "GRR", "DTW", "DTW", "DTW", "DTW", "FNT", "D…
## $ dest               <chr> "CLT", "MIA", "DFW", "BWI", "BOS", "EWR", "ATL", "D…
## $ air_time           <dbl> 88, 187, 162, 64, 71, 61, 125, 174, 105, 45, 179, 1…
## $ distance           <dbl> 500, 1214, 986, 409, 632, 488, 645, 1123, 640, 228,…
## $ hour               <dbl> 21, 5, 5, 5, 5, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6,…
## $ minute             <dbl> 15, 0, 11, 35, 45, 0, 0, 55, 0, 0, 3, 0, 0, 5, 0, 1…
## $ time_hour          <dttm> 2019-01-01 21:00:00, 2019-01-01 05:00:00, 2019-01-…
## $ temp               <dbl> 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,…
## $ humid              <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ wind_dir           <dbl> 10, 320, 290, 290, 290, 290, 330, 290, 290, 290, 29…
## $ wind_speed         <dbl> 8.05546, 9.20624, 19.56326, 19.56326, 19.56326, 13.…
## $ wind_gust          <dbl> 9.270062, 10.594357, 22.513008, 22.513008, 22.51300…
## $ precip             <dbl> NA, 1e-04, NA, NA, NA, NA, 1e-04, NA, NA, NA, NA, N…
## $ pressure           <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ visib              <dbl> 1.25, 9.00, 7.00, 7.00, 7.00, 4.00, 9.00, 7.00, 10.…
## $ carrier_name       <chr> "PSA Airlines Inc.", "Republic Airline", "American …
## $ plane_year         <dbl> 2015, 2014, 2010, 2005, 2006, 2016, 2006, 1999, 200…
## $ plane_type         <chr> "Fixed wing multi engine", "Fixed wing multi engine…
## $ plane_manufacturer <chr> "BOMBARDIER INC", "EMBRAER S A", "BOEING", "BOEING"…
## $ plane_model        <chr> "CL-600-2D24", "ERJ 170-200 LR", "737-823", "737-7H…
## $ plane_engines      <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
## $ plane_seats        <dbl> 95, 88, 162, 140, 20, 88, 95, 149, 100, 140, 162, 1…
## $ plane_speed        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ plane_engine       <chr> "Turbo-fan", "Turbo-fan", "Turbo-fan", "Turbo-fan",…

The weather values are the most missing. There’s a surprisingly large amount of departure delay times missing. There’s a lot of missing data on the conditions of the day such as weather and precipitation.

Bar Charts.

‘geom_bar()’ is when you have a categorical variable in your data set and it tallies them up for you automatically.

miFlights %>% ggplot(aes( x = origin)) + geom_bar()

This one requires you to set the variable that represents the height of your bars.

miFlights %>% count(origin) %>% ggplot(aes(x = fct_reorder(origin, -n), 
                                           y = n)) + 
  geom_col() +
  labs( title = "Michigan flights by airport, 2019-2021", 
        x = "Airport",
        y = "Number of flights",
        caption = "Data source: anyFlights R package") +
  theme_bw()

Subsetting rows using filter()

janflights <- miFlights %>% filter(month == 1,
                                   day == 1)



dec25 <- miFlights %>% filter(month == 12,
                                   day == 25)
novDec <- miFlights %>% filter(month == 11 | month == 12)
novDec <- miFlights %>% filter(month %in% 11 : 12)
notNovDec <- miFlights %>% filter(!(month %in% 11 : 12))

Missing values.

# all departure times are non-missing
miFlightsComplete <- miFlights %>% filter(!is.na(dep_time))
miFlightsMiss <- miFlights %>% filter(is.na(dep_time))

Sorting rows with arrange.

# Sort miFlights by the day of the flight (smallest to largest), and print the first 4 columns and 5 rows of the resulting data set using the slice_head() function.

miFlights %>% arrange(day) %>% select(year:sched_dep_time) %>% slice_head(n = 5)
## # A tibble: 5 × 5
##    year month   day dep_time sched_dep_time
##   <dbl> <dbl> <dbl>    <dbl>          <dbl>
## 1  2019     1     1       55           2115
## 2  2019     1     1      455            500
## 3  2019     1     1      506            511
## 4  2019     1     1      531            535
## 5  2019     1     1      534            545
# Equivalent code with slice_min()

miFlights %>% select(year:dep_time) %>%
  slice_min(day, n = 5, with_ties = FALSE)
## # A tibble: 5 × 4
##    year month   day dep_time
##   <dbl> <dbl> <dbl>    <dbl>
## 1  2019     1     1       55
## 2  2019     1     1      455
## 3  2019     1     1      506
## 4  2019     1     1      531
## 5  2019     1     1      534
miFlights %>% arrange(desc(day)) %>% select(year:sched_dep_time) %>% 
  slice_head(n = 5)
## # A tibble: 5 × 5
##    year month   day dep_time sched_dep_time
##   <dbl> <dbl> <dbl>    <dbl>          <dbl>
## 1  2019     1    31       59           2225
## 2  2019     1    31      535            545
## 3  2019     1    31      540            530
## 4  2019     1    31      548            527
## 5  2019     1    31      549            549
# Equivalent code with slice_max()

miFlights %>% select(year:dep_time) %>%
  slice_max(day, n = 5, with_ties = FALSE)
## # A tibble: 5 × 4
##    year month   day dep_time
##   <dbl> <dbl> <dbl>    <dbl>
## 1  2019     1    31       59
## 2  2019     1    31      535
## 3  2019     1    31      540
## 4  2019     1    31      548
## 5  2019     1    31      549
# Sort by year, month, and then day of the flight. 
miFlights %>% arrange(year, month, day)
## # A tibble: 463,818 × 37
##     year month   day dep_time sched_de…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
##    <dbl> <dbl> <dbl>    <dbl>      <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  
##  1  2019     1     1       55       2115     220     426    2323     303 OH     
##  2  2019     1     1      455        500      -5     830     834      -4 YX     
##  3  2019     1     1      506        511      -5     710     730     -20 AA     
##  4  2019     1     1      531        535      -4     647     710     -23 WN     
##  5  2019     1     1      534        545     -11     750     742       8 B6     
##  6  2019     1     1      550        600     -10     712     748     -36 YX     
##  7  2019     1     1      555        600      -5     822     834     -12 OO     
##  8  2019     1     1      555        555       0     709     715      -6 WN     
##  9  2019     1     1      555        600      -5     755     817     -22 DL     
## 10  2019     1     1      600        600       0     559     615     -16 WN     
## # … with 463,808 more rows, 27 more variables: flight <dbl>, tailnum <chr>,
## #   origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## #   minute <dbl>, time_hour <dttm>, temp <dbl>, dewp <dbl>, humid <dbl>,
## #   wind_dir <dbl>, wind_speed <dbl>, wind_gust <dbl>, precip <dbl>,
## #   pressure <dbl>, visib <dbl>, carrier_name <chr>, plane_year <dbl>,
## #   plane_type <chr>, plane_manufacturer <chr>, plane_model <chr>,
## #   plane_engines <dbl>, plane_seats <dbl>, plane_speed <dbl>, …
miFlights %>% arrange(desc(arr_delay), dep_delay) %>% select(arr_delay, dep_delay) %>%
  slice_head(n=3)
## # A tibble: 3 × 2
##   arr_delay dep_delay
##       <dbl>     <dbl>
## 1      2649      2672
## 2      1961      1948
## 3      1792      1840
miFlights %>%arrange(desc(arr_delay)) %>% slice_head(n=3)
## # A tibble: 3 × 37
##    year month   day dep_time sched_dep…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
##   <dbl> <dbl> <dbl>    <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  
## 1  2019     2    17     1400        1728    2672    1531    1922    2649 MQ     
## 2  2021     7    12     1643         815    1948    1754     913    1961 G4     
## 3  2019    12    19     1402         722    1840    1556    1004    1792 AA     
## # … with 27 more variables: flight <dbl>, tailnum <chr>, origin <chr>,
## #   dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>, temp <dbl>, dewp <dbl>, humid <dbl>, wind_dir <dbl>,
## #   wind_speed <dbl>, wind_gust <dbl>, precip <dbl>, pressure <dbl>,
## #   visib <dbl>, carrier_name <chr>, plane_year <dbl>, plane_type <chr>,
## #   plane_manufacturer <chr>, plane_model <chr>, plane_engines <dbl>,
## #   plane_seats <dbl>, plane_speed <dbl>, plane_engine <chr>, and abbreviated …
miFlights %>% slice_max(arr_delay, n = 3, with_ties = FALSE)
## # A tibble: 3 × 37
##    year month   day dep_time sched_dep…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
##   <dbl> <dbl> <dbl>    <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  
## 1  2019     2    17     1400        1728    2672    1531    1922    2649 MQ     
## 2  2021     7    12     1643         815    1948    1754     913    1961 G4     
## 3  2019    12    19     1402         722    1840    1556    1004    1792 AA     
## # … with 27 more variables: flight <dbl>, tailnum <chr>, origin <chr>,
## #   dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>, temp <dbl>, dewp <dbl>, humid <dbl>, wind_dir <dbl>,
## #   wind_speed <dbl>, wind_gust <dbl>, precip <dbl>, pressure <dbl>,
## #   visib <dbl>, carrier_name <chr>, plane_year <dbl>, plane_type <chr>,
## #   plane_manufacturer <chr>, plane_model <chr>, plane_engines <dbl>,
## #   plane_seats <dbl>, plane_speed <dbl>, plane_engine <chr>, and abbreviated …
miFlights %>%arrange(arr_delay) %>% slice_head(n=3)
## # A tibble: 3 × 37
##    year month   day dep_time sched_dep…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
##   <dbl> <dbl> <dbl>    <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  
## 1  2019     1    13     1001        1000       1    1129    1254     -85 DL     
## 2  2019     1    26     1255        1257      -2    1445    1606     -81 DL     
## 3  2021     1    11      848         855      -7     951    1110     -79 DL     
## # … with 27 more variables: flight <dbl>, tailnum <chr>, origin <chr>,
## #   dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>, temp <dbl>, dewp <dbl>, humid <dbl>, wind_dir <dbl>,
## #   wind_speed <dbl>, wind_gust <dbl>, precip <dbl>, pressure <dbl>,
## #   visib <dbl>, carrier_name <chr>, plane_year <dbl>, plane_type <chr>,
## #   plane_manufacturer <chr>, plane_model <chr>, plane_engines <dbl>,
## #   plane_seats <dbl>, plane_speed <dbl>, plane_engine <chr>, and abbreviated …
miFlights %>% slice_min(arr_delay, n = 3, with_ties = FALSE)
## # A tibble: 3 × 37
##    year month   day dep_time sched_dep…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
##   <dbl> <dbl> <dbl>    <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  
## 1  2019     1    13     1001        1000       1    1129    1254     -85 DL     
## 2  2019     1    26     1255        1257      -2    1445    1606     -81 DL     
## 3  2021     1    11      848         855      -7     951    1110     -79 DL     
## # … with 27 more variables: flight <dbl>, tailnum <chr>, origin <chr>,
## #   dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>, temp <dbl>, dewp <dbl>, humid <dbl>, wind_dir <dbl>,
## #   wind_speed <dbl>, wind_gust <dbl>, precip <dbl>, pressure <dbl>,
## #   visib <dbl>, carrier_name <chr>, plane_year <dbl>, plane_type <chr>,
## #   plane_manufacturer <chr>, plane_model <chr>, plane_engines <dbl>,
## #   plane_seats <dbl>, plane_speed <dbl>, plane_engine <chr>, and abbreviated …
miFlights %>% slice_max(distance / air_time, n = 3, with_ties = FALSE)
## # A tibble: 3 × 37
##    year month   day dep_time sched_dep…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
##   <dbl> <dbl> <dbl>    <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  
## 1  2019     7    14     1230        1215      15    1339    1326      13 OO     
## 2  2020     8    21     1645        1605      40    1621    1626      -5 OO     
## 3  2021    11    21     1000        1000       0    1119    1132     -13 OO     
## # … with 27 more variables: flight <dbl>, tailnum <chr>, origin <chr>,
## #   dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>, temp <dbl>, dewp <dbl>, humid <dbl>, wind_dir <dbl>,
## #   wind_speed <dbl>, wind_gust <dbl>, precip <dbl>, pressure <dbl>,
## #   visib <dbl>, carrier_name <chr>, plane_year <dbl>, plane_type <chr>,
## #   plane_manufacturer <chr>, plane_model <chr>, plane_engines <dbl>,
## #   plane_seats <dbl>, plane_speed <dbl>, plane_engine <chr>, and abbreviated …
miFlights %>% select(-year, -month)
## # A tibble: 463,818 × 35
##      day dep_time sched…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier flight tailnum
##    <dbl>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>    <dbl> <chr>  
##  1     1       55    2115     220     426    2323     303 OH        1019 N567NN 
##  2     1      455     500      -5     830     834      -4 YX         954 N433YX 
##  3     1      506     511      -5     710     730     -20 AA         185 N853NN 
##  4     1      531     535      -4     647     710     -23 WN         203 N227WN 
##  5     1      534     545     -11     750     742       8 B6         310 N203JB 
##  6     1      550     600     -10     712     748     -36 YX         790 N739YX 
##  7     1      555     600      -5     822     834     -12 OO         803 N817SK 
##  8     1      555     555       0     709     715      -6 WN         295 N7818L 
##  9     1      555     600      -5     755     817     -22 DL         348 N899AT 
## 10     1      600     600       0     559     615     -16 WN         218 N906WN 
## # … with 463,808 more rows, 25 more variables: origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>,
## #   temp <dbl>, dewp <dbl>, humid <dbl>, wind_dir <dbl>, wind_speed <dbl>,
## #   wind_gust <dbl>, precip <dbl>, pressure <dbl>, visib <dbl>,
## #   carrier_name <chr>, plane_year <dbl>, plane_type <chr>,
## #   plane_manufacturer <chr>, plane_model <chr>, plane_engines <dbl>,
## #   plane_seats <dbl>, plane_speed <dbl>, plane_engine <chr>, and abbreviated …
miFlights %>% select(time_hour, air_time, everything(), -day)
## # A tibble: 463,818 × 36
##    time_hour           air_time  year month dep_time sched_dep…¹ dep_d…² arr_t…³
##    <dttm>                 <dbl> <dbl> <dbl>    <dbl>       <dbl>   <dbl>   <dbl>
##  1 2019-01-01 21:00:00       88  2019     1       55        2115     220     426
##  2 2019-01-01 05:00:00      187  2019     1      455         500      -5     830
##  3 2019-01-01 05:00:00      162  2019     1      506         511      -5     710
##  4 2019-01-01 05:00:00       64  2019     1      531         535      -4     647
##  5 2019-01-01 05:00:00       71  2019     1      534         545     -11     750
##  6 2019-01-01 06:00:00       61  2019     1      550         600     -10     712
##  7 2019-01-01 06:00:00      125  2019     1      555         600      -5     822
##  8 2019-01-01 05:00:00      174  2019     1      555         555       0     709
##  9 2019-01-01 06:00:00      105  2019     1      555         600      -5     755
## 10 2019-01-01 06:00:00       45  2019     1      600         600       0     559
## # … with 463,808 more rows, 28 more variables: sched_arr_time <dbl>,
## #   arr_delay <dbl>, carrier <chr>, flight <dbl>, tailnum <chr>, origin <chr>,
## #   dest <chr>, distance <dbl>, hour <dbl>, minute <dbl>, temp <dbl>,
## #   dewp <dbl>, humid <dbl>, wind_dir <dbl>, wind_speed <dbl>, wind_gust <dbl>,
## #   precip <dbl>, pressure <dbl>, visib <dbl>, carrier_name <chr>,
## #   plane_year <dbl>, plane_type <chr>, plane_manufacturer <chr>,
## #   plane_model <chr>, plane_engines <dbl>, plane_seats <dbl>, …
#Create a subset of the miFlights data set called timeFlights that only contains variables that end with the word "time". 

timeFlights <- miFlights %>% select(ends_with("time"))

timeFlights %>% slice_head(n=5)
## # A tibble: 5 × 5
##   dep_time sched_dep_time arr_time sched_arr_time air_time
##      <dbl>          <dbl>    <dbl>          <dbl>    <dbl>
## 1       55           2115      426           2323       88
## 2      455            500      830            834      187
## 3      506            511      710            730      162
## 4      531            535      647            710       64
## 5      534            545      750            742       71
departureInfo <- miFlights %>% select(starts_with("dep"))

departureInfo %>% slice_head(n=5)
## # A tibble: 5 × 2
##   dep_time dep_delay
##      <dbl>     <dbl>
## 1       55       220
## 2      455        -5
## 3      506        -5
## 4      531        -4
## 5      534       -11
newFlights <- miFlights %>% select(flight, origin, dest, everything(),
                                   -tailnum)

Mutate Function.

flights_sml <- miFlights %>% select(ends_with("delay"), distance, air_time)

flights_sml %>% mutate(gain = dep_delay - arr_delay) %>% 
  slice_head(n = 5)
## # A tibble: 5 × 5
##   dep_delay arr_delay distance air_time  gain
##       <dbl>     <dbl>    <dbl>    <dbl> <dbl>
## 1       220       303      500       88   -83
## 2        -5        -4     1214      187    -1
## 3        -5       -20      986      162    15
## 4        -4       -23      409       64    19
## 5       -11         8      632       71   -19
flightSpeeds <- miFlights %>% select(ends_with("delay"), distance, air_time) %>% mutate(gain = dep_delay - arr_delay,
           speed = distance / air_time*60)

flightSpeeds %>% slice_head(n=5)
## # A tibble: 5 × 6
##   dep_delay arr_delay distance air_time  gain speed
##       <dbl>     <dbl>    <dbl>    <dbl> <dbl> <dbl>
## 1       220       303      500       88   -83  341.
## 2        -5        -4     1214      187    -1  390.
## 3        -5       -20      986      162    15  365.
## 4        -4       -23      409       64    19  383.
## 5       -11         8      632       71   -19  534.
flightSpeeds %>% ggplot(aes( x = speed,
                             y = gain, 
                             color = distance)) + 
  geom_point(alpha = 0.1) +
  labs(title = "Michigan flights, 2019-2021",
       x = "Speed (mph)",
       y = "Gain (minutes)",
       caption = "Data source: anyFlights R package", 
       color =  "Distance(miles)") + 
  theme_bw()+ 
  theme(legend.position = "bottom")
## Warning: Removed 10239 rows containing missing values (geom_point).

miFlights %>% group_by(year, month, day) %>% 
summarize(delay = mean(dep_delay, na.rm = TRUE)) %>% 
  slice_head(n = 5)
## `summarise()` has grouped output by 'year', 'month'. You can override using the
## `.groups` argument.
## # A tibble: 180 × 4
## # Groups:   year, month [36]
##     year month   day  delay
##    <dbl> <dbl> <dbl>  <dbl>
##  1  2019     1     1  8.58 
##  2  2019     1     2 12.5  
##  3  2019     1     3  0.721
##  4  2019     1     4 -0.477
##  5  2019     1     5  2.17 
##  6  2019     2     1 27.8  
##  7  2019     2     2 13.0  
##  8  2019     2     3  1.49 
##  9  2019     2     4  7.91 
## 10  2019     2     5  1.91 
## # … with 170 more rows
delaySummary <- miFlights %>% group_by(carrier_name) %>% 
  summarize(Delay = mean(arr_delay, na.rm = T))

delaySummary %>% ggplot(aes( x= fct_reorder(carrier_name, Delay),
                             y = Delay, fill = Delay > 0)) +
  geom_col() +
  labs (title = "Average flight delays by carrier",
        subtitle = "Michigan Flights, 2019-2021",
        x = "Carrier",
        y = "Delay (minutes)",
        caption = "Data source: anyflights R package") +
  scale_fill_manual(values = c("#D55E00", "#0072B2")) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45,
                                   vjust =  0.7), 
        legend.position = "none")

miFlights %>% group_by(month, origin) %>%
  summarize(Delay = median(arr_delay, na.rm = TRUE)) %>%
  ggplot(aes(x = month, y = Delay, color = origin)) + 
  geom_line() +
  labs (title = "Average flight delays by month",
        subtitle = "Michigan Flights, 2019-2021",
        x = "Month",
        y = "Delay (minutes)",
        caption = "Data source: anyflights R package") +
  scale_x_continuous(breaks = 1:12) +
  theme_bw()
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.