title: Generating tables for each factor level over a range of columns, in R with dplyr and reshape2
body: <p>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. </p>
  
  <p>Example data frame:</p>
  
  <pre><code>df &lt;- 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))
  </code></pre>
  
  <p>df Returns:</p>
  
  <pre><code>  Organization Gender Q1a Q1b
  1            A   Male   1   1
  2            B Female   2   2
  3            C   Male   3   1
  4            D Female   4   2
  </code></pre>
  
  <p>Here is the function I wrote:</p>
  
  <pre><code>library(dplyr)
  library(reshape2)
  f = function(df) {
    a &lt;- which(colnames(df) == "Q1a")
    for(i in colnames(df[, a:length(df)])) {
        levelsList &lt;- as.list(levels(i))
        for(j in levelsList) {
            temp &lt;- subset(df, i == j, select = c(Organization, Gender, i))
            temp &lt;- group_by(temp, Organization, Gender)
            table &lt;- summarize(temp, count = n())
            table &lt;- dcast(table, Organization ~ Gender)
            table &lt;- mutate(table, Total = Female + Male)
            write.csv(table, paste0(i, "_", j, ".csv"))
        }
     }
  }
  </code></pre>
  
  <p>The expected output for a larger dataset would look like:</p>
  
  <pre><code>    Organization    Female  Male    Total
  1          A          366    259      625
  2          B          30     35       65
  3          C          83     61       144
  4          D          55     58       113
  </code></pre>
  
  <p>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?</p>