The count function from dplyr package is one of my favorites for data wrangling.
I like to include a percent column to the function results.
penguins %>%
count(species, sort = T) %>%
mutate(percent = scales::percent(n/sum(n), scale = 100))
# A tibble: 3 × 3
species n percent
<fct> <int> <chr>
1 Adelie 152 44.2%
2 Gentoo 124 36.0%
3 Chinstrap 68 19.8%
Whenever more than one column will be counted it is nice to write a small function for that.
count_percent <- function(df, a_column) {
df %>%
count({{a_column}}) %>%
mutate(percent = scales::percent(n/sum(n), scale = 100))
}
penguins %>%
count_percent(species)
# A tibble: 3 × 3
species n percent
<fct> <int> <chr>
1 Adelie 152 44.2%
2 Chinstrap 68 19.8%
3 Gentoo 124 36.0%
penguins %>%
count_percent(island)
# A tibble: 3 × 3
island n percent
<fct> <int> <chr>
1 Biscoe 168 49%
2 Dream 124 36%
3 Torgersen 52 15%
Para esta função funcionar é preciso utilizar o operador curly-curly.
Citation
BibTeX citation:
@misc{abreu2020,
author = {Abreu, Marcos},
title = {R tip: Counting percentual function with curly-curly},
date = {2020-10-10},
url = {https://abreums.github.io/posts/2022-10-10-r-tip-counting-percentual-function-with-curly-curly/},
langid = {pt-br}
}
For attribution, please cite this work as:
Abreu, Marcos. 2020.
“R tip: Counting percentual function with
curly-curly.” October 10, 2020.
https://abreums.github.io/posts/2022-10-10-r-tip-counting-percentual-function-with-curly-curly/.