how to count the total number of competitors on an airport? Need coding help in R

Viewed 40

I am reviewing a dataset on flights and airport details. The dataset contains the following columns with the data about airport and flight details. I am new to coding in R so I need help

flight_date op_career tail_num flight_num origin origin_airport_ID dest_airport_ID dest
03/13/2019 AA N900EV 3503 SFO 11308 10397 LAX
03/13/2019 AA N900EZ 3502 SFO 11308 10397 LAX
03/13/2019 AS N686BR 3397 SFO 11308 10397 LAX
03/13/2019 YV N932LR 5804 SFO 11308 10397 LAX
03/14/2019 DL 255NV 515 SFO 11308 10397 LAX

I want to find the number of competitors on each airport

so in this case a summary output would look something like this

Airport competitors
SFO 5
LAX 5

Any help would be greatly appreciated. Thanks in advance :)

1 Answers

We could use

library(dplyr)
library(tidyr)
df1 %>%
  select(op_career, origin, dest) %>%
  pivot_longer(cols = -op_career, names_to = NULL, values_to = 'Airport') %>% 
  dplyr::count(Airport, name = 'competitors')

-output

# A tibble: 2 × 2
  Airport competitors
  <chr>         <int>
1 LAX               5
2 SFO               5
Related