title: Iterate through columns and row values (list) in R dplyr
body: <p>This question is based on the following post with additional requirements (<a href="https://stackoverflow.com/questions/39833762/iterate-through-columns-in-dplyr">Iterate through columns in dplyr?</a>).</p>
  
  <p>The original code is as follows:</p>
  
  <pre><code>df &lt;- data.frame(col1 = rep(1, 15),
        col2 = rep(2, 15),
        col3 = rep(3, 15),
        group = c(rep("A", 5), rep("B", 5), rep("C", 5)))
  
  for(col in c("col1", "col2", "col3")){
        filt.df &lt;- df %&gt;%
        filter(group == "A") %&gt;% 
        select_(.dots = c('group', col))
        # do other things, like ggplotting
        print(filt.df)
  }
  </code></pre>
  
  <p>My objective is to output a frequency table for each unique COL by GROUP combination. The current example specifies a dplyr filter based on a GROUP value A, B, or C. In my case, I want to iterate (loop) through a list of values in GROUP (list &lt;- c("A", "B", "C") and generate a frequency table for each combination.</p>
  
  <p>The frequency table is based on counts. For Col1 the result would look something like the table below. The example data set is simplified. My real dataset is more complex with multiple 'values' per 'group'. I need to iterate through Col1-Col3 by group.</p>
  
  <p>group  value  n  prop<br>
  A &nbsp; &nbsp; &nbsp;  1 &nbsp; &nbsp; &nbsp;      5&nbsp; &nbsp; &nbsp;    .1<br>
  B &nbsp; &nbsp; &nbsp;      2 &nbsp; &nbsp; &nbsp;      5 &nbsp; &nbsp; &nbsp;   .1<br>
  C &nbsp; &nbsp; &nbsp;      3 &nbsp; &nbsp; &nbsp;      5&nbsp; &nbsp; &nbsp;    .1 </p>
  
  <p>A better example of the frequency table is here: <a href="https://stackoverflow.com/questions/34860535/how-to-use-dplyr-to-generate-a-frequency-table/34860724?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa">How to use dplyr to generate a frequency table</a></p>
  
  <p>I struggled with this for a couple days, and I could have done better with my example. Thanks for the posts. Here is what I ended up doing to solve this. The result is a series of frequency tables for each column and each unique value found in group. I had 3 columns (col1, col2, col3) and 3 unique values in group (A,B,C), 3x3. The result is 9 frequency tables and a frequency table for each  group value that is non-sensical. I am sure there is a better way to do this. The output generates some labeling, which is useful.</p>
  
  <pre><code># Build unique group list
  group &lt;- unique(df$group)
  
  # Generate frequency tables via a loop
  iterate_by_group &lt;- function(x)
   for (i in 1:length(group)){ 
    filt.df &lt;- df[df$group==group[i],]
    print(lapply(filt.df, freq))
  }
  
  # Run 
  iterate_by_group(df)
  </code></pre>