library(magrittr)
library(dplyr)
library(stringr)
df <- mtcars; df$car <- rownames(df)
df %>%
mutate(make = str_extract(car, "^[A-Za-z]+\\b")) %>%
group_by(make) %>%
summarize(n = n()) %>%
mutate(proportion = n/sum(n)) %>%
arrange(desc(proportion)) %>%
top_n2(5, proportion, make) %>%
select(-n) %>%
mutate(proportion = sprintf("%.2f%%", 100*proportion)) %>%
knitr::kable()| make | proportion |
|---|---|
| Merc | 21.88% |
| Fiat | 6.25% |
| Hornet | 6.25% |
| Mazda | 6.25% |
| Toyota | 6.25% |
| ...17 others... | 53.12% |