title: Remove rows for var2 = x where var1=0 and row below has var1=1
body: <p>I have a dataframe like this:</p>
  <pre><code>df&lt;-data.frame(client_id = c(7, 7, 7, 17, 17, 17, 19, 
                 19, 19,19,19), delivery = c(0,0,1,0,0,1,0,0,1,1,1), order_id = c(&quot;snshw&quot;,&quot;nsnsjjw&quot;,&quot;smssjjw&quot;,&quot;jsnn&quot;,&quot;ksksues&quot;,&quot;ncbf&quot;,&quot;nsngw&quot;,&quot;manhsm&quot;,&quot;opltkg&quot;,&quot;snsjue&quot;,&quot;msmssje&quot;), which_order = c(1, 2, 3, 1, 2, 
   
  </code></pre>
  <p>And I need to remove rows for each client_id where delivery=0 and a row below has delivery = 1. The dataset after such an operation would look like this:</p>
  <pre><code>  df&lt;- data.frame(client_id = c(7, 7, 17, 17, 
                       19, 19,19,19), delivery = c(0,1,0,1,0,1,1,1), order_id = c(&quot;snshw&quot;,&quot;smssjjw&quot;,&quot;jsnn&quot;,&quot;ncbf&quot;,&quot;nsngw&quot;,&quot;opltkg&quot;,&quot;snsjue&quot;,&quot;msmssje&quot;), which_order = c(1, 3, 1, 
                                                                                                                                                                        3, 1,3,4,5))
  </code></pre>
  <p>Do you have any idea what might work? I've tried some dplyr functions but didn't come up with anything useful.</p>
  <p>Probably there is no need to solve this for each client_id as there is very low probability that the first row for any client_id has delivery=1 (and therefore low risk that we would delete some rows with higher client_id).</p>