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.– Rui Barradas
I edited for 'Titanic', thanks for the remark.
– r_rabbit