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.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.4 ✔ 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
Create the code that makes a table for each of the below
questions.
1. join + filter - Which airplanes fly LGA to XNA (1 POINT)
Q.1 <- flights %>%
filter(origin == "LGA", dest == "XNA") %>%
inner_join(planes, by = "tailnum") %>%
select(tailnum, manufacturer, model, year.y, type, engine, engines, seats) %>%
distinct() %>%
arrange(tailnum)
Q.1
## # A tibble: 4 × 8
## tailnum manufacturer model year.y type engine engines seats
## <chr> <chr> <chr> <int> <chr> <chr> <int> <int>
## 1 N711MQ GULFSTREAM AEROSPACE G1159B 1976 Fixed wi… Turbo… 2 22
## 2 N713EV BOMBARDIER INC CL-600-2C10 2003 Fixed wi… Turbo… 2 80
## 3 N737MQ CESSNA 172N 1977 Fixed wi… Recip… 1 4
## 4 N840MQ CANADAIR LTD CF-5D 1974 Fixed wi… Turbo… 4 2
2. join - Add the airline name to the flights table (1 POINT)
Q.2 <- flights %>%
left_join(airlines, by = "carrier") %>%
select(year, month, day, name, carrier, tailnum, flight, dep_time, sched_dep_time, dep_delay, arr_time, sched_arr_time, arr_delay, origin, dest, air_time, distance, hour, minute, time_hour)
Q.2
## # A tibble: 336,776 × 20
## year month day name carrier tailnum flight dep_time sched_dep_time
## <int> <int> <int> <chr> <chr> <chr> <int> <int> <int>
## 1 2013 1 1 United Air … UA N14228 1545 517 515
## 2 2013 1 1 United Air … UA N24211 1714 533 529
## 3 2013 1 1 American Ai… AA N619AA 1141 542 540
## 4 2013 1 1 JetBlue Air… B6 N804JB 725 544 545
## 5 2013 1 1 Delta Air L… DL N668DN 461 554 600
## 6 2013 1 1 United Air … UA N39463 1696 554 558
## 7 2013 1 1 JetBlue Air… B6 N516JB 507 555 600
## 8 2013 1 1 ExpressJet … EV N829AS 5708 557 600
## 9 2013 1 1 JetBlue Air… B6 N593JB 79 557 600
## 10 2013 1 1 American Ai… AA N3ALAA 301 558 600
## # ℹ 336,766 more rows
## # ℹ 11 more variables: dep_delay <dbl>, arr_time <int>, sched_arr_time <int>,
## # arr_delay <dbl>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
3. join + select + distinct() - Which airports have no commercial
flights (1 POINT)
used_airports <- flights %>%
select(faa = origin) %>%
distinct() %>%
bind_rows(flights %>% select(faa = dest) %>% distinct()) %>%
distinct()
Q.3 <- airports %>%
anti_join(used_airports, by = "faa") %>%
select(faa, name, tzone)
Q.3
## # A tibble: 1,355 × 3
## faa name tzone
## <chr> <chr> <chr>
## 1 04G Lansdowne Airport America/New_York
## 2 06A Moton Field Municipal Airport America/Chicago
## 3 06C Schaumburg Regional America/Chicago
## 4 06N Randall Airport America/New_York
## 5 09J Jekyll Island Airport America/New_York
## 6 0A9 Elizabethton Municipal Airport America/New_York
## 7 0G6 Williams County Airport America/New_York
## 8 0G7 Finger Lakes Regional Airport America/New_York
## 9 0P2 Shoestring Aviation Airfield America/New_York
## 10 0S9 Jefferson County Intl America/Los_Angeles
## # ℹ 1,345 more rows