How Get Share of Values on a Grouped Tibble

Hey team!

I'm trying to summarize a table but also showing alongside the count of values (categorical) i wanted to see what % they represent over the total. I'm grouping the data so when i call the n() function it counts only the categories within each group how i do: n()/"overall count of variable"?

Here is one method.

DF <- tibble(Cat = sample(LETTERS[1:4], 20, replace = TRUE))
Percents <- DF %>% group_by(Cat) %>% summarize(C = n(), P = n()/nrow(DF))
Percents
# A tibble: 4 × 3
  Cat       C     P
  <chr> <int> <dbl>
1 A         5  0.25
2 B         7  0.35
3 C         2  0.1 
4 D         6  0.3 
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.