title: R Concatenate Across Rows Within Groups but Preserve Sequence
body: <p>My data consists of text from many dyads that has been split into sentences, one per row.  I'd like to concatenate the data by speaker within dyads, essentially converting the data to speaking turns.  Here's an example data set:</p>
  <pre><code>dyad &lt;- c(1,1,1,1,1,2,2,2,2)
  speaker &lt;- c(&quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;Paul&quot;,&quot;John&quot;, &quot;George&quot;, &quot;Ringo&quot;, &quot;Ringo&quot;, &quot;George&quot;)
  text &lt;- c(&quot;Let's play&quot;,
            &quot;We're wasting time&quot;,
            &quot;Let's make a record!&quot;,
            &quot;Let's work it out first&quot;,
            &quot;Why?&quot;,
            &quot;It goes like this&quot;,
            &quot;Hold on&quot;,
            &quot;Have to tighten my snare&quot;,
            &quot;Ready?&quot;)
  
  dat &lt;- data.frame(dyad, speaker, text)
  </code></pre>
  <p>And this is what I'd like the data to look like:</p>
  <pre><code>  dyad speaker                                                text
  1      1    John Let's play. We're wasting time. Let's make a record!
  2      1    Paul                              Let's work it out first
  3      1    John                                                 Why?
  4      2  George                                    It goes like this
  5      2   Ringo                    Hold on. Have to tighten my snare
  6      2  George                                               Ready?
  </code></pre>
  <p>I've tried grouping by sender and pasting/collapsing from <em>dplyr</em> but the concatenation combines all of a sender's text without preserving speaking turn order. For example, John's last statement (&quot;Why&quot;) winds up with his other text in the output rather than coming after Paul's comment. I also tried to check if the next speaker (using <em>lead(sender)</em>) is the same as current and then combining, but it only does adjacent rows, in which case it misses John's third comment in the example. Seems it should be simple but I can't make it happen.  And it should be flexible to combine any series of continuous rows by a given speaker.</p>
  <p>Thanks in advance</p>