For a range of columns (variables) in a data frame, I need to generate tables for each level of each variable. I have created a function that finds the first column in the range (Q1a) and then runs a for loop for every column after. The loop finds the number of levels in each column and then a second for loop is supposed to generate the table and save it as a .csv with a unique file name.
Example data frame:
df <- data.frame(Organization = c("A", "B", "C", "D"), Gender = c("Male", "Female", "Male", "Female"), Q1a = c(1, 2, 3, 4), Q1b = c(1, 2, 1, 2))
df Returns:
Organization Gender Q1a Q1b
1 A Male 1 1
2 B Female 2 2
3 C Male 3 1
4 D Female 4 2
Here is the function I wrote:
library(dplyr)
library(reshape2)
f = function(df) {
a <- which(colnames(df) == "Q1a")
for(i in colnames(df[, a:length(df)])) {
levelsList <- as.list(levels(i))
for(j in levelsList) {
temp <- subset(df, i == j, select = c(Organization, Gender, i))
temp <- group_by(temp, Organization, Gender)
table <- summarize(temp, count = n())
table <- dcast(table, Organization ~ Gender)
table <- mutate(table, Total = Female + Male)
write.csv(table, paste0(i, "_", j, ".csv"))
}
}
}
The expected output for a larger dataset would look like:
Organization Female Male Total
1 A 366 259 625
2 B 30 35 65
3 C 83 61 144
4 D 55 58 113
I can generate a single table like the one above correctly with a few lines of code. The problem is that the function does not generate any files. Am I on the right track? Is there an easier way to do this using apply?