Mean as a window-function in dplyr : <p>I want to calculate group means but save them as a vector next to the raw data. Is there an efficient way to do this in <code>dplyr</code>?</p>

<p>Original <code>data.frame</code>: </p>

<pre><code>dat &lt;- data.frame(
  group = c("A","A","A","B","B","B"),
  value = c(4, 5, 6, 7, 8, 9)
  )

&gt; dat
  group value
1     A     4
2     A     5
3     A     6
4     B     7
5     B     8
6     B     9
</code></pre>

<p>Desired <code>data.frame</code> with full vector of group means:</p>

<pre><code>dat2 &lt;- data.frame(
  group = c("A","A","A","B","B","B"),
  value = c(4, 5, 6, 7, 8, 9),
  groupmeanvalue = c(5, 5, 5, 8, 8, 8)
)

&gt; dat2
  group value groupmeanvalue
1     A     4              5
2     A     5              5
3     A     6              5
4     B     7              8
5     B     8              8
6     B     9              8
</code></pre>
