title: R loop to produce multiple summary tables and graphs
body: <p>what I want to do is to plot the percentage of observations with a specific value by the group. The following code produces a graph I want, but I want to do this same function over multiple variables instead of copying and pasting, and changing the variable name manually.</p>
  <p>The following is how my dataset looks like. There are three dichotomous variables (x1,x2,x3) that I want to plot and one variable that indicates a group.</p>
  <pre><code>data &lt;- data.frame(group = sample(c(&quot;Tesla&quot;, &quot;Niro&quot;, &quot;Leaf&quot;), 50, replace=TRUE, prob=c(0.40, 0.20, 0.40)), x1 = rbinom(n=50, size=1, prob=0.3), x2 = rbinom(n=50, size=1, prob=0.5), 
                     x3 = rbinom(n=50, size=1, prob=0.5))
  </code></pre>
  <p>To plot the percentage of observations with a value of 1, I did some data wrangling to get a summary table and plotted the summary table.</p>
  <pre><code>summary &lt;- data %&gt;% 
    group_by(group) %&gt;%
    summarise(Obs = n()) ## gets the number of observations in each group 
  
  x1 &lt;- data %&gt;% 
    filter(x1 == 1) %&gt;%
    group_by(group) %&gt;%
    summarise(Freq = n()) ## counts the number of observations with value 1 in each group 
  
  ## make dataframe 
  x1_df &lt;- left_join((summary), x1, by = &quot;group&quot;) ## combines the two dataframe above 
  
  ## create percentage 
  x1_df &lt;- x1_df %&gt;% mutate(Percentage = Freq/Obs*100) ## calculates the percentage of 1 in each group 
  
  # create a variable that indicates a specific group 
  x1_df &lt;- x1_df %&gt;% mutate(Tesla = as.factor(ifelse(group == &quot;Tesla&quot;, 1, 0))) # want to color a certain group (i.e., Tesla) in a different color in a graph 
  
  png(&quot;g_x1.png&quot;)
  g_x1 &lt;- ggbarplot(subset(x1_df, !is.na(Percentage)), x = &quot;group&quot;, y = &quot;Percentage&quot;,
                      fill = &quot;Tesla&quot;,               # change fill color by cyl
                      #  color = &quot;white&quot;,            # Set bar border colors to white
                      palette = &quot;grey&quot;,            # jco journal color palett. see ?ggpar
                      sort.val = &quot;desc&quot;,          # Sort the value in dscending order
                      sort.by.groups = FALSE,     # Don't sort inside each group
                      x.text.angle = 90           # Rotate vertically x axis texts
  ) 
  print(g_x1)
  dev.off()
  </code></pre>
  <p>The output is the following graph.<br />
  [1]: <a href="https://i.stack.imgur.com/0xidT.png" rel="nofollow noreferrer">https://i.stack.imgur.com/0xidT.png</a></p>
  <p>I want to do the same and get a graph for x2, x3, and many more. I did some search, and it seems like I can either use for loop or lapply. But I haven't seen an attempt to save the looped output in individual dataframes.</p>
  <p>This is how I'm thinking about this.</p>
  <pre><code>## summary table of observations 
  summary &lt;- data %&gt;% 
    group_by(group) %&gt;%
    summarise(Obs = n())
  
  summary &lt;- summary %&gt;% 
    add_column(x1 = NA, 
               x2 = NA, 
               x3 = NA)
  
  for(i in 1:ncol(data)){
    summary[,i+1] &lt;- data[,i+1] %&gt;%
      filter(data[,i+1] == 1) %&gt;%
      group_by(group) %&gt;%
      summarize(Freq = n())
  }
  </code></pre>
  <p>But it does not work, and gives this error &quot;Error: unexpected '}' in &quot;}&quot;</p>