title: R: Restructure a dataframe
body: <p>I have several objects with an <code>id</code> (in the below example 1-3). For each of these objects every day their <code>status</code> is logged.</p>
  <p>Some sample data:</p>
  <pre class="lang-r prettyprint-override"><code>data &lt;- data.frame(id =     c(1, 1, 1, 1, 1,  2, 2, 2,  3, 3, 3, 3),
                     status = c(1, 2, 2, 3, 4,  1, 2, 3,  1, 2, 3, 4 ),
                     date = c(&quot;2021-01-01&quot;, &quot;2021-01-02&quot;, &quot;2021-01-03&quot;, &quot;2021-01-04&quot;, &quot;2021-01-05&quot;,
                              &quot;2021-01-02&quot;, &quot;2021-01-03&quot;, &quot;2021-01-04&quot;,
                              &quot;2021-01-01&quot;, &quot;2021-01-02&quot;, &quot;2021-01-03&quot;, &quot;2021-01-04&quot;))
  
  data
  </code></pre>
  <p>This looks like this:</p>
  <pre><code>   id status       date
  1   1      1 2021-01-01
  2   1      2 2021-01-02
  3   1      2 2021-01-03
  4   1      3 2021-01-04
  5   1      4 2021-01-05
  6   2      1 2021-01-02
  7   2      2 2021-01-03
  8   2      3 2021-01-04
  9   3      1 2021-01-01
  10  3      2 2021-01-02
  11  3      3 2021-01-03
  12  3      4 2021-01-04
  
  </code></pre>
  <p>For example on January second the status of object 1 turned from 1 to 2. On january third the status of object 1 didn't change. On January fourth it changed to status 3 etc.</p>
  <p>How can I restructure that table, so that I have the changes between two days (<code>from</code> = the status of the day before, <code>to</code> = the status of the day after) in a form like this?</p>
  <pre><code>id  from   to   date
  1   1      2    2021-01-02
  1   2      2    2021-01-03
  1   2      3    2021-01-04
  1   3      4    2021-01-05
  
  2   1      2    2021-01-03
  2   2      3    2021-01-04
  
  3   1      2    2021-01-02
  3   2      3    2021-01-03
  3   3      4    2021-01-04 
  
  </code></pre>