title: Table of categorical variables by a grouping variable in R
body: <p>I have a dataset with some categorical variables + a "cluster" variable. For example:</p>
  
  <pre><code>time &lt;- c("Morning", "Evening" ,"Morning", "Morning", "Afternoon", "Evening", "Afternoon")
  dollar &lt;- c("1-5", "6-10", "11-15", "1-5", "1-5", "6-10", "6-10")
  with_kids &lt;- c("no", "yes", "yes", "no", "no", "yes", "yes")
  cluster &lt;- c(1,1,2,3,2,2,3)
  
  data &lt;- cbind(time, dollar, with_kids, cluster)
  </code></pre>
  
  <p>How can I create a frequency table of all the categorical variables by "cluster"?</p>
  
  <p><a href="https://i.stack.imgur.com/e5j4U.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e5j4U.jpg" alt="Desired Output -&gt; table on the right (i.e. % within each cluster, for each categorical variable"></a></p>
  
  <p>Desired output is the table on the right (column % of each categorical variable within each cluster).</p>
  
  <p>I know this code will work for one variable. What is the most efficient way to do it if I have many more categorical variables?</p>
  
  <pre><code>table(data$time, data$cluster)
  </code></pre>