library(nycflights13)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── 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
flights_lga_xna <- flights %>%
filter(origin == "LGA", dest == "XNA") %>%
select(flight, tailnum)
flights_lga_xna
## # A tibble: 745 × 2
## flight tailnum
## <int> <chr>
## 1 4534 N722MQ
## 2 4525 N719MQ
## 3 4413 N739MQ
## 4 4534 N719MQ
## 5 4525 N711MQ
## 6 4413 N723MQ
## 7 4534 N711MQ
## 8 4525 N730MQ
## 9 4413 N722MQ
## 10 4534 N719MQ
## # ℹ 735 more rows
head(flights_lga_xna)
## # A tibble: 6 × 2
## flight tailnum
## <int> <chr>
## 1 4534 N722MQ
## 2 4525 N719MQ
## 3 4413 N739MQ
## 4 4534 N719MQ
## 5 4525 N711MQ
## 6 4413 N723MQ
Question 2
flights_with_airline <- flights %>%
left_join(airlines, by = "carrier") %>%
select(flight, airline_name = name, everything())
flights_with_airline
## # A tibble: 336,776 × 20
## flight airline_name year month day dep_time sched_dep_time dep_delay
## <int> <chr> <int> <int> <int> <int> <int> <dbl>
## 1 1545 United Air Lines … 2013 1 1 517 515 2
## 2 1714 United Air Lines … 2013 1 1 533 529 4
## 3 1141 American Airlines… 2013 1 1 542 540 2
## 4 725 JetBlue Airways 2013 1 1 544 545 -1
## 5 461 Delta Air Lines I… 2013 1 1 554 600 -6
## 6 1696 United Air Lines … 2013 1 1 554 558 -4
## 7 507 JetBlue Airways 2013 1 1 555 600 -5
## 8 5708 ExpressJet Airlin… 2013 1 1 557 600 -3
## 9 79 JetBlue Airways 2013 1 1 557 600 -3
## 10 301 American Airlines… 2013 1 1 558 600 -2
## # ℹ 336,766 more rows
## # ℹ 12 more variables: arr_time <int>, sched_arr_time <int>, arr_delay <dbl>,
## # carrier <chr>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
## # distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
head(flights_with_airline)
## # A tibble: 6 × 20
## flight airline_name year month day dep_time sched_dep_time dep_delay
## <int> <chr> <int> <int> <int> <int> <int> <dbl>
## 1 1545 United Air Lines I… 2013 1 1 517 515 2
## 2 1714 United Air Lines I… 2013 1 1 533 529 4
## 3 1141 American Airlines … 2013 1 1 542 540 2
## 4 725 JetBlue Airways 2013 1 1 544 545 -1
## 5 461 Delta Air Lines In… 2013 1 1 554 600 -6
## 6 1696 United Air Lines I… 2013 1 1 554 558 -4
## # ℹ 12 more variables: arr_time <int>, sched_arr_time <int>, arr_delay <dbl>,
## # carrier <chr>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
## # distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
Question 3
airports_no_flights <- airports %>%
left_join(flights, by = c("faa" = "origin")) %>%
filter(is.na(flight)) %>%
select(name) %>%
distinct()
airports_no_flights
## # A tibble: 1,437 × 1
## name
## <chr>
## 1 Lansdowne Airport
## 2 Moton Field Municipal Airport
## 3 Schaumburg Regional
## 4 Randall Airport
## 5 Jekyll Island Airport
## 6 Elizabethton Municipal Airport
## 7 Williams County Airport
## 8 Finger Lakes Regional Airport
## 9 Shoestring Aviation Airfield
## 10 Jefferson County Intl
## # ℹ 1,427 more rows
head(airports_no_flights)
## # A tibble: 6 × 1
## name
## <chr>
## 1 Lansdowne Airport
## 2 Moton Field Municipal Airport
## 3 Schaumburg Regional
## 4 Randall Airport
## 5 Jekyll Island Airport
## 6 Elizabethton Municipal Airport