Concatenate strings by group with dplyr
<p>i have a dataframe that looks like this</p>

<pre><code>&gt; data &lt;- data.frame(foo=c(1, 1, 2, 3, 3, 3), bar=c('a', 'b', 'a', 'b', 'c', 'd'))
&gt; data
  foo bar
1   1   a
2   1   b
3   2   a
4   3   b
5   3   c
6   3   d
</code></pre>

<p>I would like to create a new column bars_by_foo which is the concatenation of the values of bar by foo. So the new data should look like this:</p>

<pre><code>  foo bar bars_by_foo
1   1   a          ab
2   1   b          ab
3   2   a           a
4   3   b         bcd
5   3   c         bcd
6   3   d         bcd
</code></pre>

<p>I was hoping that the following would work:</p>

<pre><code>p &lt;- function(v) {
  Reduce(f=paste, x = v)
}
data %&gt;% 
  group_by(foo) %&gt;% 
  mutate(bars_by_foo=p(bar))
</code></pre>

<p>But that code gives me an error</p>

<p><code>Error: incompatible types, expecting a character vector</code>.</p>

<p>What am I doing wrong?</p>