title: creating variables for proportions in each category, over multiple variables
body: <p>I want to create a data frame with columns for the proportion of observations in each category, much like this:</p>
  
  <pre><code>library(tidyverse)
  mtcars %&gt;%
    group_by(am) %&gt;%
    summarise(gear3 = sum(gear == 3)/n(), 
              gear4 = sum(gear == 4)/n(), 
              gear5 = sum(gear == 5)/n(), 
              cyl4 = sum(cyl == 4)/n(),
              cyl6 = sum(cyl == 6)/n(),
              cyl8 = sum(cyl == 8)/n())
  
  # # A tibble: 2 x 7
  #      am gear3 gear4 gear5  cyl4  cyl6  cyl8
  #   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  # 1     0 0.789 0.211 0     0.158 0.211 0.632
  # 2     1 0     0.615 0.385 0.615 0.231 0.154
  </code></pre>
  
  <p>I am looking for way to this without manually naming the new summary variables?  </p>
  
  <p>There seems to be a few questions, such as <a href="https://stackoverflow.com/questions/24576515/relative-frequencies-proportions-with-dplyr">here</a>, related to creating a proportions for single variables, which i could replicate for each variable, pivot and and then combine but it will become tedious in my application - i am trying to build the data frame for many variables</p>
  
  <pre><code>mtcars %&gt;%
    group_by(am, gear) %&gt;%
    summarise(n = n()) %&gt;%
    mutate(freq = n / sum(n))
  # # A tibble: 4 x 4
  # # Groups:   am [2]
  #      am  gear     n  freq
  #   &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  # 1     0     3    15 0.789
  # 2     0     4     4 0.211
  # 3     1     4     8 0.615
  # 4     1     5     5 0.385
  
  mtcars %&gt;%
    group_by(am, cyl) %&gt;%
    summarise(n = n()) %&gt;%
    mutate(freq = n / sum(n))
  # # A tibble: 6 x 4
  # # Groups:   am [2]
  #      am   cyl     n  freq
  #   &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  # 1     0     4     3 0.158
  # 2     0     6     4 0.211
  # 3     0     8    12 0.632
  # 4     1     4     8 0.615
  # 5     1     6     3 0.231
  # 6     1     8     2 0.154
  </code></pre>