title: dplyr: put count occurrences into new variable
body: <p>Would like to get a hand on dplyr code, but cannot figure this out. Have seen a similar issue described here for many variables (<a href="https://stackoverflow.com/questions/25811756/summarizing-counts-of-a-factor-with-dplyr">summarizing counts of a factor with dplyr</a> and <a href="https://stackoverflow.com/questions/23838322/putting-rowwise-counts-of-value-occurences-into-new-variables-how-to-do-that-in">Putting rowwise counts of value occurences into new variables, how to do that in R with dplyr?</a>), however my task is somewhat smaller.<br>
  Given a data frame, how do I count the frequency of a variable and place that in a new variable.</p>
  
  <pre><code>set.seed(9)
  df &lt;- data.frame(
      group=c(rep(1,5), rep(2,5)),
      var1=round(runif(10,1,3),0))
  </code></pre>
  
  <p>Then we have:</p>
  
  <pre><code>&gt;df
     group var1
  1      1    1
  2      1    1
  3      1    1
  4      1    1
  5      1    2
  6      2    1
  7      2    2
  8      2    2
  9      2    2
  10     2    3
  </code></pre>
  
  <p>Would like a third column indicating per-group (<code>group</code>) how many times <code>var1</code> occurs, in this example this would be: count=(4,4,4,4,1,1,3,3,3,1).
  I tried - without success - things like: </p>
  
  <pre><code>df %&gt;%  group_by(group) %&gt;% rowwise() %&gt;% do(count = nrow(.$var1))
  </code></pre>
  
  <p>Explanations are very appreciated!</p>