2

I am working with the "nyclfights13" package and the "tidyverse" package.

In the "planes" data set found in "nycflights13", there is a column that tells you the manufacturer of the plane.

How would I find out which are the 5 most common manufacturers?

Phil
  • 7,287
  • 3
  • 36
  • 66

2 Answers2

3

We can use count and get the top 5

library(dplyr)
planes %>% 
   count(manufacturer) %>%
   top_n(5)

Or with slice

planes %>% 
    count(manufacturer) %>%
    arrange(desc(n)) %>% 
    slice(1:5)
# A tibble: 5 x 2
#  manufacturer         n
#  <chr>            <int>
#1 BOEING            1630
#2 AIRBUS INDUSTRIE   400
#3 BOMBARDIER INC     368
#4 AIRBUS             336
#5 EMBRAER            299
akrun
  • 874,273
  • 37
  • 540
  • 662
2

A base R solution

tail(sort(table(planes$manufacturer)), 5)

 EMBRAER    AIRBUS   BOMBARDIER INC AIRBUS INDUSTRIE      BOEING 
  299        336         368             400              1630 
G5W
  • 36,531
  • 10
  • 47
  • 80