with a data frame df
like below
df <- data.frame(colors = c("red", "blue", "green", "red", "red" , "blue"))
I can find out the count per color using dplyr as follows
df %>%
group_by(color) %>%
summarise(count = n())
Instead of count I need to find the percentage count for each color - how to go about it using dplyr ?
You can either pipe this to a mutate( prop = count / sum(count) )
or directly within summarise
with nrow(.)
. Something like this:
df %>%
group_by(colors) %>%
summarise(count = n() / nrow(.) )
or
df %>%
group_by(colors) %>%
summarise(count = n() ) %>%
mutate( prop = count / sum(count) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With