The table
function in base R is really helpful in creating counts of categorical variables; however, the output is a specific table object - thus it is not recognized by almost any subsequent functions within R that would prove useful (ggplot
, kable
, etc.).
Here's a function that creates a list comprising the count of each level within the factors and converts them to data frames.
#df should be a data structure containing the factors of interest
freqList = lapply(df,
function(x) {
my_lst = data.frame(table(x))
names(my_lst) = c("level", "n")
return(my_lst)
}
)
freqList
Calling freqList
will print the full list. Each column/variable will be its own data frame object.