title: find sequences from rows of data
body: <p>I'm working with a dataframe containing a series of &quot;events&quot; and the dates on which they occurred. These events are sequential, where one follows another, but there are multiple separate sequences.</p>
  <p>The data is structured so that each row shows an event (<code>event_name</code>) and an event that followed it at some point later on in the sequence (<code>later_event_name</code>). See below:</p>
  <pre><code>   event_name  event_date later_event_name later_event_date
  1           A  2020-01-01                B       2020-02-01
  2           A  2020-01-01                C       2020-03-01
  3           A  2020-01-01                G       2020-07-01
  4           B  2020-02-01                C       2020-03-01
  5           B  2020-02-01                G       2020-07-01
  6           C  2020-03-01                G       2020-07-01
  7           D  2020-04-01                F       2020-06-01
  8           D  2020-04-01                I       2020-09-01
  9           F  2020-06-01                I       2020-09-01
  10          E  2020-05-01                H       2020-08-01
  </code></pre>
  <p>In this case, <code>F</code> and <code>I</code> both follow <code>D</code>, and <code>I</code> follows <code>F</code>, so the sequence is <code>D</code>-&gt;<code>F</code>-&gt;<code>I</code>.</p>
  <p>There are three sequences in this dataframe:</p>
  <ol>
  <li><code>A</code>-&gt;<code>B</code>-&gt;<code>C</code>-&gt;<code>G</code></li>
  <li><code>D</code>-&gt;<code>F</code>-&gt;<code>I</code></li>
  <li><code>E</code>-&gt;<code>H</code></li>
  </ol>
  <p>My actual data comprises thousands of rows. I'd like to be able to automate the process of grouping the events that comprise each sequence. An idea final output would be as follows:</p>
  <pre><code># the desired output
  # `seq_id` is the identifier for each separate sequence
  # `seq` shows the order of each event within that sequence
    seq_id seq event_name event_date
  1      1   1          A 2020-01-01
  2      1   2          B 2020-02-01
  3      1   3          C 2020-03-01
  4      1   4          G 2020-07-01
  5      2   1          D 2020-04-01
  6      2   2          F 2020-06-01
  7      2   3          I 2020-09-01
  8      3   1          E 2020-05-01
  9      3   2          H 2020-08-01
  </code></pre>
  <p>Here is the dummy data:</p>
  <pre><code># the input data
  structure(list(event_name = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;D&quot;, &quot;F&quot;, &quot;E&quot;),
                 event_date = structure(c(18262, 18262, 18262, 
                                          18293, 18293, 18322, 18353, 18353, 18414, 18383), class = &quot;Date&quot;),
                 later_event_name = c(&quot;B&quot;, &quot;C&quot;, &quot;G&quot;, &quot;C&quot;, &quot;G&quot;, &quot;G&quot;, &quot;F&quot;, &quot;I&quot;, &quot;I&quot;, &quot;H&quot;),
                 later_event_date = structure(c(18293, 18322, 18444, 
                                                18322, 18444, 18444, 18414, 18506, 18506, 18475), class = &quot;Date&quot;)),
            row.names = c(NA, -10L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>Some notes:</p>
  <ul>
  <li>sequences can be any length from two upwards.</li>
  <li>For simplicity I've here named the events &quot;A&quot;, &quot;B&quot;, and so on, but in reality their IDs are alphanumeric codes with no logical pattern or sequence.</li>
  </ul>