Suppose I want to calculate the proportion of different values within each group. For example, using the mtcars data, how do I calculate the relative f
mtcars
Here is a general function implementing Henrik's solution on dplyr 0.7.1.
dplyr
freq_table <- function(x, group_var, prop_var) { group_var <- enquo(group_var) prop_var <- enquo(prop_var) x %>% group_by(!!group_var, !!prop_var) %>% summarise(n = n()) %>% mutate(freq = n /sum(n)) %>% ungroup }