title: dplyr: How to calculate frequency of different values within each group
body: <p>I am probably having a failry easy question but cannnot figure it out.</p>
  <p>I am having a dataset that has two variables, both factors. It looks like this:</p>
  <pre><code>my.data&lt;-data.frame(name=c(&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;b&quot;,&quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;e&quot;, &quot;e&quot;, &quot;e&quot;),
                  var1=c(1, 2, 3, 4, 2, 1, 4, 1, 3, 4, 3))
  </code></pre>
  <p>I would like to calculate the frequency of 1,2,3 and 4 for all a, b and e aggregated later into one row. That means that all &quot;a&quot;, &quot;b&quot; and &quot;e&quot; should be in one row and then I would like to create 4 variables which will indicate the frequency of all 1,2,3 and 4 across these rows. I have managed to calculate the frequencies for all counts of &quot;a&quot;, &quot;b&quot; and &quot;e&quot; but I can't collapse all the &quot;a&quot;, &quot;b&quot; and &quot;e&quot; into seperate rows.</p>
  <p>My code is this one:</p>
  <pre><code>a &lt;- my.data %&gt;%
  dplyr:: select(name, var1) %&gt;%
  mutate(name = as.factor(name),
       var1 = as.factor(var1)) %&gt;% 
  group_by(name, var1) %&gt;%
  summarise(n = n()) %&gt;%
  mutate(freq = n / sum(n))
  </code></pre>
  <p>My results should look like this:</p>
  <pre><code>name   Freq1   Freq2   Freq3   Freq4
    a    0,00    0,00    0,5     0,5
    b    0,30    0,30    0,30    0,10
    e    0,20    0,20    0,20    0,40
  </code></pre>
  <p>Thanks.</p>