library(nycflights13)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Question 1

q1_lga_xna_planes <- flights %>%
  filter(origin == "LGA", dest == "XNA") %>%
  select(tailnum) %>%
  distinct() %>%
  inner_join(planes, by = "tailnum") %>%
  arrange(tailnum) 

head(q1_lga_xna_planes)
## # A tibble: 4 × 9
##   tailnum  year type               manufacturer model engines seats speed engine
##   <chr>   <int> <chr>              <chr>        <chr>   <int> <int> <int> <chr> 
## 1 N711MQ   1976 Fixed wing multi … GULFSTREAM … G115…       2    22    NA Turbo…
## 2 N713EV   2003 Fixed wing multi … BOMBARDIER … CL-6…       2    80    NA Turbo…
## 3 N737MQ   1977 Fixed wing single… CESSNA       172N        1     4   105 Recip…
## 4 N840MQ   1974 Fixed wing multi … CANADAIR LTD CF-5D       4     2    NA Turbo…

Question 2

q2_flights_with_airline <- flights %>%
  left_join(airlines, by = "carrier") %>%
  select(year, month, day, carrier, name, flight, origin, dest)

head(q2_flights_with_airline)
## # A tibble: 6 × 8
##    year month   day carrier name                   flight origin dest 
##   <int> <int> <int> <chr>   <chr>                   <int> <chr>  <chr>
## 1  2013     1     1 UA      United Air Lines Inc.    1545 EWR    IAH  
## 2  2013     1     1 UA      United Air Lines Inc.    1714 LGA    IAH  
## 3  2013     1     1 AA      American Airlines Inc.   1141 JFK    MIA  
## 4  2013     1     1 B6      JetBlue Airways           725 JFK    BQN  
## 5  2013     1     1 DL      Delta Air Lines Inc.      461 LGA    ATL  
## 6  2013     1     1 UA      United Air Lines Inc.    1696 EWR    ORD

Question 3

airports_with_flights <- flights %>%
  select(faa = origin) %>%
  bind_rows(flights %>% select(faa = dest)) %>%
  distinct()

q3_airports_no_commercial_flights <- airports %>%
  anti_join(airports_with_flights, by = "faa") %>%
  select(faa, name, lat, lon)

head(q3_airports_no_commercial_flights)
## # A tibble: 6 × 4
##   faa   name                             lat   lon
##   <chr> <chr>                          <dbl> <dbl>
## 1 04G   Lansdowne Airport               41.1 -80.6
## 2 06A   Moton Field Municipal Airport   32.5 -85.7
## 3 06C   Schaumburg Regional             42.0 -88.1
## 4 06N   Randall Airport                 41.4 -74.4
## 5 09J   Jekyll Island Airport           31.1 -81.4
## 6 0A9   Elizabethton Municipal Airport  36.4 -82.2

Question 4

q4_airports_most_high_winds <- weather %>%
  filter(!is.na(wind_speed), wind_speed > 30) %>%
  count(origin, name = "high_wind_events") %>%
  filter(high_wind_events == max(high_wind_events)) %>%
  left_join(airports %>% select(faa, name), by = c("origin" = "faa")) %>%
  select(name) %>%
  distinct()

head(q4_airports_most_high_winds)
## # A tibble: 1 × 1
##   name               
##   <chr>              
## 1 John F Kennedy Intl