Calculate percentage with dplyr::add_count

Asked

Viewed 79 times

2

how it would be possible to calculate percentages with the dplyr::add_count()?

To calculate with the dplyr::count() is very simple, as follows:

library(tidyverse)
dt <- Titanic

dt %>% 
  count(Sex, Pclass, sort = T) %>% 
  mutate(perc = n/sum(n)*100) 

However, this option does not preserve the other variables and I would like to preserve them. The dplyr::add_count does this, but I could not calculate the percentages in the same way as above, because the counting values repeat and the calculation ends up not making sense.

dt %>% 
  add_count(Sex, Pclass, sort = T) %>% 
  mutate(perc = n/sum(n)*100)

Would anyone have any suggestions? I also tried with the group_by(), but without success. Thank you!

  • Is not reproducible: Error: object 'titanic' not found.

  • I edited for 'Titanic', thanks for the remark.

1 answer

6

After the code with count, make a join to add the remaining columns.

dt %>%
  count(Sex, Pclass, sort = TRUE) %>%
  mutate(perc = n/sum(n)*100) %>%
  inner_join(dt, .)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.