title: Select specific rows based on previous row value (in the same column)
body: <p>I've been trying to figure a way to script this through R, but just can't get it. I have a dataset like this: </p>
  
  <pre><code>Trial  Type Correct Latency     
  1       55  0       0
  3       30  1       766
  4       10  1       344
  6       40  1       716
  7       10  1       326
  9       30  1       550
  10      10  1       350
  11      64  0       0
  13      30  1       683
  14      10  1       270
  16      30  1       666
  17      10  1       297
  19      40  1       616
  20      10  1       315
  21      64  0       0
  23      40  1       850
  24      10  1       322
  26      30  1       566
  27      20  0       766
  28      40  1       500
  29      20  1       230
  </code></pre>
  
  <p>which goes for much longer(around 1000 rows).</p>
  
  <p>From this one dataset, I would like to create 4 separate data.frames/tables I can export tables with as well as do my own calculations </p>
  
  <p>I would like to have a data.frame (4 in total), one for each of these bullet points:</p>
  
  <ul>
  <li>type 10 rows which are preceded by a type 30 row </li>
  <li>type 10 rows which are preceded by a type 40 row</li>
  <li>type 20 rows which are preceded by a type 30 row</li>
  <li>type 20 rows which are preceded by a type 40 row </li>
  </ul>
  
  <p>I would like for all the columns in the relevant rows to be placed into these new tables, but only including the column info of row types 10 or 20.</p>
  
  <p>For example, the first table (<strong>type 10 preceded by type 30</strong>) would like this based on the sample data:</p>
  
  <pre><code>Trial  Type Correct Latency     
    4       10     1       344
    10      10     1       350
    14      10     1       270
    17      10     1       297
  </code></pre>
  
  <p>Second table (<strong>type 10 preceded by type 40</strong>):</p>
  
  <pre><code>Trial    Type  Correct  Latency     
    7       10     1       326
    20      10     1       315
    24      10     1       322
  </code></pre>
  
  <p>Third table (<strong>type 20 preceded by type 30</strong>):</p>
  
  <pre><code>Trial    Type  Correct  Latency     
    27      20     0       766
  </code></pre>
  
  <p>Fourth table (<strong>table 20 preceded by type 40</strong>):</p>
  
  <pre><code>Trial    Type  Correct   Latency        
   29      20      1        230
  </code></pre>
  
  <p>I can subset just fine to get one table only of type 10 rows and another for type 20 rows, but I can't figure out how to create different tables for type 10 and 20 rows based on the previous type value. Also, an issue is that "Trials" is not in order (skips numbers).</p>
  
  <p>Any help would be greatly appreciated. Thank you. </p>
  
  <p><strong>Also</strong>, is there a way to include the previous row as well, so the output for the fourth table would look something like this:</p>
  
  <p>Fourth table (<strong>table 20 preceded by type 40</strong>):</p>
  
  <pre><code>Trial    Type  Correct   Latency        
   28      40      1        500
   29      20      1        230
  </code></pre>