Here's a tidyverse version:
library(tidyverse)
data(diamonds)
(as.data.frame(table(diamonds$cut)) %>% rename(Count=1,Freq=2) %>% mutate(Perc=100*Freq/sum(Freq)))
Or if you want a handy function:
getPercentages <- function(df, colName) {
df.cnt <- df %>% select({{colName}}) %>%
table() %>%
as.data.frame() %>%
rename({{colName}} :=1, Freq=2) %>%
mutate(Perc=100*Freq/sum(Freq))
}
Now you can do:
diamonds %>% getPercentages(cut)
or this:
df=diamonds %>% group_by(cut) %>% group_modify(~.x %>% getPercentages(clarity))
ggplot(df,aes(x=clarity,y=Perc))+geom_col()+facet_wrap(~cut)