title: incosistent relative frequency outputs with summarise and mutate
body: <p>I want to compute relative frequency of a group of values with respect to the remaining groups. For example, compute the relative frequency of <code>gear==3</code> in <code>am==0</code>. I have computed using the following way.</p>
  
  <pre><code>library(dplyr)
  
  mtcars %&gt;% 
    select(am, gear) %&gt;% 
    group_by(am, gear) %&gt;% 
    summarise(N = n()) %&gt;%
    group_by(am) %&gt;% 
    mutate(freq = N / sum(N))
  
  # Source: local data frame [4 x 4]
  # Groups: am [2]
  # 
  # # A tibble: 4 x 4
  #     am  gear     N      freq
  #   &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt;     &lt;dbl&gt;
  # 1     0     3    15 0.7894737
  # 2     0     4     4 0.2105263
  # 3     1     4     8 0.6153846
  # 4     1     5     5 0.3846154
  </code></pre>
  
  <p>The above output is as expected. However, I would like the <code>freq</code> values as a new column in original dataset with same values. I tried the below approach for calculating the count <code>Ǹ</code> and then relative frequency <code>freq</code>. </p>
  
  <pre><code>mtcars %&gt;% 
    select(am, gear) %&gt;% 
    group_by(am, gear) %&gt;% 
    mutate(N = n()) %&gt;%
    group_by(am) %&gt;% 
    mutate(freq = N / sum(N))
  
  # Source: local data frame [32 x 4]
  # Groups: am [2]
  # 
  # # A tibble: 32 x 4
  #      am  gear     N       freq
  #   &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt;      &lt;dbl&gt;
  # 1     1     4     8 0.08988764
  # 2     1     4     8 0.08988764
  # 3     1     4     8 0.08988764
  # 4     0     3    15 0.06224066
  # 5     0     3    15 0.06224066
  # 6     0     3    15 0.06224066
  # 7     0     3    15 0.06224066
  # 8     0     4     4 0.01659751
  # 9     0     4     4 0.01659751
  # 10     0     4     4 0.01659751
  # # ... with 22 more rows
  </code></pre>
  
  <p>Now, it gives a different output. What might be the reason? </p>