library(dplyr) # for the %>% pipe
mtcars %>%
crosstab(am, cyl) %>%
ns_to_percents(denom = "all")
# when total_n is needed
mtcars %>%
crosstab(am, cyl) %>%
add_totals_row() %>% # add a totals row that should not be included in the denominator
ns_to_percents(denom = "all", total_n = nrow(mtcars)) # specify correct denominator
Run the code above in your browser using DataLab