I have a contingency table of counts, and I want to extend it with corresponding proportions of each group.
Some sample data (tips data set from gg
Here is another example using the lapply and table functions in base R.
freqList = lapply(select_if(tips, is.factor),
function(x) {
df = data.frame(table(x))
df = data.frame(fct = df[, 1],
n = sapply(df[, 2], function(y) {
round(y / nrow(dat), 2)
}
)
)
return(df)
}
)
Use print(freqList) to see the proportion tables (percent of frequencies) for each column/feature/variable (depending on your tradecraft) that is labeled as a factor.