title: Create frequency tables for multiple factor columns in R
body: <p>I am a novice in R. I am compiling a separate manual on the syntax for the common functions/features for my work. My sample dataframe as follows:</p>
  
  <pre><code>x.sample &lt;-
  structure(list(Q9_A = structure(c(5L, 3L, 5L, 3L, 5L, 3L, 1L, 
  5L, 5L, 5L), .Label = c("Impt", "Neutral", "Not Impt at all", 
  "Somewhat Impt", "Very Impt"), class = "factor"), Q9_B = structure(c(5L, 
  5L, 5L, 3L, 5L, 5L, 3L, 5L, 3L, 3L), .Label = c("Impt", "Neutral", 
  "Not Impt at all", "Somewhat Impt", "Very Impt"), class = "factor"), 
  Q9_C = structure(c(3L, 5L, 5L, 3L, 5L, 5L, 3L, 5L, 5L, 3L
  ), .Label = c("Impt", "Neutral", "Not Impt at all", "Somewhat Impt", 
  "Very Impt"), class = "factor")), .Names = c("Q9_A", "Q9_B", 
  "Q9_C"), row.names = c(NA, 10L), class = "data.frame")
  
  &gt; x.sample
            Q9_A            Q9_B            Q9_C
  1        Very Impt       Very Impt Not Impt at all
  2  Not Impt at all       Very Impt       Very Impt
  3        Very Impt       Very Impt       Very Impt
  4  Not Impt at all Not Impt at all Not Impt at all
  5        Very Impt       Very Impt       Very Impt
  6  Not Impt at all       Very Impt       Very Impt
  7             Impt Not Impt at all Not Impt at all
  8        Very Impt       Very Impt       Very Impt
  9        Very Impt Not Impt at all       Very Impt
  10       Very Impt Not Impt at all Not Impt at all
  </code></pre>
  
  <p>My original dataframe has 21 columns.</p>
  
  <p>If I want to find the mean (treating this as an ordinal variable):</p>
  
  <pre><code>&gt; sapply(x.sample,function(x) mean(as.numeric(x), na.rm=TRUE))
  Q9_A Q9_B Q9_C 
   4.0  4.2  4.2
  </code></pre>
  
  <p>I would like to tabulate a frequency table for ALL the variables in my dataframe. I searched the internet and many forums and saw that the nearest command to do this is using sapply. But when I did it, it gave all 0s.</p>
  
  <pre><code>&gt; sapply(x.sample,function(x) table(factor(x.sample, levels=c("Not Impt at all", "Somewhat Impt",            "Neutral", "Impt", "Very Impt"), ordered=TRUE)))
                  Q9_A Q9_B Q9_C
  Not Impt at all    0    0    0
  Somewhat Impt      0    0    0
  Neutral            0    0    0
  Impt               0    0    0
  Very Impt          0    0    0
  </code></pre>
  
  <p>QUESTION
  How can I make use of sapply to tabulate a frequency chart as per the above table for all the columns (that are factors) in a dataframe?</p>
  
  <p>PS So sorry if this seems trivia but I have searched for 2 days without an answer and trying all possible combinations. Maybe I didn't search hard enough =(</p>
  
  <p>Thanks very much.</p>