Problem 4 (Medium): Use the nycflights13 package and the flights and planes tables to answer the following questions: a. What is the oldest plane (specified by the tailnum variable) that flew from New York City airports in 2013? b. How many airplanes that flew from New York City are included in the planes table?
时间: 2024-01-31 18:01:45 浏览: 120
a. To answer this question, we need to join the flights table with the planes table on the tailnum variable and filter for flights that departed in 2013 from New York City airports. Then, we can sort by the year variable in the planes table and select the oldest plane. Here's the code:
```{r}
library(nycflights13)
oldest_plane <- flights %>%
inner_join(planes, by = "tailnum") %>%
filter(origin %in% c("EWR", "JFK", "LGA") & year == 2013) %>%
arrange(year) %>%
select(tailnum, year) %>%
distinct() %>%
slice_head(n = 1)
oldest_plane
```
The output shows that the oldest plane that flew from New York City airports in 2013 is tail number "N201AA" with a manufacturing year of 1956.
```
# A tibble: 1 x 2
tailnum year
<chr> <int>
1 N201AA 1956
```
b. To answer this question, we need to count the number of unique tail numbers in the flights table that departed from New York City airports in 2013 and match them with the planes table. Here's the code:
```{r}
num_planes <- flights %>%
filter(origin %in% c("EWR", "JFK", "LGA") & year == 2013) %>%
distinct(tailnum) %>%
inner_join(planes, by = "tailnum") %>%
summarize(num_planes = n_distinct(tailnum))
num_planes
```
The output shows that there are 404 airplanes that flew from New York City airports in 2013 and are included in the planes table.
```
# A tibble: 1 x 1
num_planes
<int>
1 404
```
阅读全文