States
States

Reputation: 163

Find aiports that have flights connected to them

library(tidyverse)
library(nycflights13)

I want to find out which airports have flights to them. My attempt is seen below, but it is not correct (it yields a number that is way bigger than the amount of airports)

airPortFlights <- airports %>% rename(dest=faa) %>% left_join(flights, "dest"=faa)

If anyone wonders why I do the rename above, that's because it won't let me do

airports %>% left_join(flights, "dest"=faa)

It gives

Error: by required, because the data sources have no common variables`

I even tried airports %>% left_join(flights, by=c("dest"=faa)) and several other attempts, which are also not working.

Thanks in advance.

Upvotes: 0

Views: 68

Answers (1)

Edward
Edward

Reputation: 18683

You want an inner_join and then either count the distinct flights, or just list the airports using distinct. Here I count them.

library(dplyr)

inner_join(airports, flights, by=c("faa"="dest")) %>%
  count(faa, name) %>% # number of flights
  arrange(-n)

# A tibble: 101 x 3
   faa   name                                   n
   <chr> <chr>                              <int>
 1 ORD   Chicago Ohare Intl                 17283
 2 ATL   Hartsfield Jackson Atlanta Intl    17215
 3 LAX   Los Angeles Intl                   16174
 4 BOS   General Edward Lawrence Logan Intl 15508
 5 MCO   Orlando Intl                       14082
 6 CLT   Charlotte Douglas Intl             14064
 7 SFO   San Francisco Intl                 13331
 8 FLL   Fort Lauderdale Hollywood Intl     12055
 9 MIA   Miami Intl                         11728
10 DCA   Ronald Reagan Washington Natl       9705
# ... with 91 more rows

So 101 of the 1,458 airports in this dataset have at least 1 record in the flights dataset, with Chicago's O'Hare Intl airport having the most flights from New York.

And just for fun, the following lists the airports that don't have any flights from NY:

anti_join(airports, flights, by=c("faa"="dest"))

Upvotes: 1

Related Questions