Create frequency tables for multiple factor columns in R

You were nearly there. Just one small change in your function would have got you there. The x in function(x) … needs to be passed through to the table() call: levs <- c(“Not Impt at all”, “Somewhat Impt”, “Neutral”, “Impt”, “Very Impt”) sapply(x.sample, function(x) table(factor(x, levels=levs, ordered=TRUE))) A little re-jig of the code might make … Read more

Means multiple columns by multiple groups [duplicate]

We can use dplyr with summarise_at to get mean of the concerned columns after grouping by the column of interest library(dplyr) airquality %>% group_by(City, year) %>% summarise_at(vars(“PM25”, “Ozone”, “CO2”), mean) Or using the devel version of dplyr (version – ‘0.8.99.9000’) airquality %>% group_by(City, year) %>% summarise(across(PM25:CO2, mean))