I am trying to add total row after calculating count and proportion. I have created a function for the same but it's returning error when passing !!sym(group_var)
freq_table <- function(df, group_var) {
group_var2 <- !!sym(group_var)
df %>%
group_by_at(group_var) %>%
summarise(n = n()) %>%
mutate(freq = n / sum(n)) %>%
ungroup %>%
bind_rows(., data.frame(
group_var2 := "Grand Total",
sapply(. %>% dplyr::select_if(is.numeric),
function(x) sum(x, na.rm = T)) %>% t()
))
}
freq_table(iris, group_var = "Species")