library(nycflights13)
library(tidyverse)

1. join + filter - Which airplanes fly LGA to XNA (1 POINT)

q1 <- planes %>% 
  left_join(flights, by = "tailnum") %>% 
  filter(origin == "LGA", dest == "XNA") %>% 
  select(year.x , type , manufacturer , model , tailnum , origin , dest) %>% 
  distinct(model, .keep_all = TRUE) 

print(q1) 
## # A tibble: 4 × 7
##   year.x type                     manufacturer        model tailnum origin dest 
##    <int> <chr>                    <chr>               <chr> <chr>   <chr>  <chr>
## 1   1976 Fixed wing multi engine  GULFSTREAM AEROSPA… G115… N711MQ  LGA    XNA  
## 2   2003 Fixed wing multi engine  BOMBARDIER INC      CL-6… N713EV  LGA    XNA  
## 3   1977 Fixed wing single engine CESSNA              172N  N737MQ  LGA    XNA  
## 4   1974 Fixed wing multi engine  CANADAIR LTD        CF-5D N840MQ  LGA    XNA

2. join - Add the airline name to the flight s table (1 POINT)

q2 <- flights %>% 
  left_join(airlines , by = "carrier") 

print(select(q2, carrier , flight , tailnum , origin , dest , name)) 
## # A tibble: 336,776 × 6
##    carrier flight tailnum origin dest  name                    
##    <chr>    <int> <chr>   <chr>  <chr> <chr>                   
##  1 UA        1545 N14228  EWR    IAH   United Air Lines Inc.   
##  2 UA        1714 N24211  LGA    IAH   United Air Lines Inc.   
##  3 AA        1141 N619AA  JFK    MIA   American Airlines Inc.  
##  4 B6         725 N804JB  JFK    BQN   JetBlue Airways         
##  5 DL         461 N668DN  LGA    ATL   Delta Air Lines Inc.    
##  6 UA        1696 N39463  EWR    ORD   United Air Lines Inc.   
##  7 B6         507 N516JB  EWR    FLL   JetBlue Airways         
##  8 EV        5708 N829AS  LGA    IAD   ExpressJet Airlines Inc.
##  9 B6          79 N593JB  JFK    MCO   JetBlue Airways         
## 10 AA         301 N3ALAA  LGA    ORD   American Airlines Inc.  
## # ℹ 336,766 more rows

3. join + select + distinct() - Which airports have no commercial flights (1 POINT)

q3 <- airports %>%
  anti_join(flights, by = c("faa" = "origin")) %>%  
  anti_join(flights, by = c("faa" = "dest")) %>% 
  distinct(faa, name, lat, lon) 

print(q3) 
## # A tibble: 1,355 × 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
##  7 0G6   Williams County Airport         41.5  -84.5
##  8 0G7   Finger Lakes Regional Airport   42.9  -76.8
##  9 0P2   Shoestring Aviation Airfield    39.8  -76.6
## 10 0S9   Jefferson County Intl           48.1 -123. 
## # ℹ 1,345 more rows

4. EXTRA CREDIT Create a table with the names of the airports with the most winds

q4 <- airports %>%
  inner_join(weather, by = c("faa" = "origin")) %>%
  filter(wind_speed > 30) %>% 
  select(faa, name , wind_speed) %>% 
  distinct(faa, .keep_all = TRUE)

print(q4) 
## # A tibble: 3 × 3
##   faa   name                wind_speed
##   <chr> <chr>                    <dbl>
## 1 EWR   Newark Liberty Intl       31.1
## 2 JFK   John F Kennedy Intl       32.2
## 3 LGA   La Guardia                31.1