title: Replace cells when sequence of row elements identified in data frame
body: <p>I currently have a dataset which can be simplified as follows: </p>
  
  <pre><code>df &lt;- data.frame(c(1,1,1,2,2,2,3,3,3),c(TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE), 
             c(0,3,0,5,5,0,0,0,7), c("a","b","c","d","a","b","c","d","a"))
  colnames(df) &lt;- c("ID", "Status", "Number", "Letter")
  
    ID Status Number Letter
  1  1   TRUE      0      a
  2  1  FALSE      3      b
  3  1   TRUE      0      c
  4  2  FALSE      5      d
  5  2  FALSE      5      a
  6  2   TRUE      0      b
  7  3   TRUE      0      c
  8  3   TRUE      0      d
  9  3  FALSE      7      a
  </code></pre>
  
  <p>Basically, I want to identify the IDs for which TRUE occurs before FALSE. Then, I want to replace the Status and Number in the rows that show TRUE (i.e. rows 1,7,8) by the subsequent FALSE row (i.e. rows 2,9,9). The final result should look as follows:</p>
  
  <pre><code>  ID Status Number Letter
  1  1  FALSE      3      a
  2  1  FALSE      3      b
  3  1   TRUE      0      c
  4  2  FALSE      5      d
  5  2  FALSE      5      a
  6  2   TRUE      0      b
  7  3  FALSE      7      c
  8  3  FALSE      7      d
  9  3  FALSE      7      a
  </code></pre>
  
  <p>The last column only indicates that I have other variables that are observation-specific and I cannot simply replace the entire row by the subsequent one.</p>
  
  <p>So far, from this post (<a href="https://stackoverflow.com/questions/41130912/r-identify-a-sequence-of-row-elements-by-groups-in-a-dataframe">R - Identify a sequence of row elements by groups in a dataframe</a>), I managed to obtain the first part:</p>
  
  <pre><code>library(tidyverse)
  extract &lt;- df %&gt;% group_by(ID) %&gt;%
    filter(ifelse(Status == FALSE,
                  lag(Status) == TRUE,
                  lead(Status) == FALSE)) 
  
  # A tibble: 4 x 4
  # Groups:   ID [2]
       ID Status Number Letter
    &lt;dbl&gt; &lt;lgl&gt;   &lt;dbl&gt; &lt;fct&gt; 
  1    1. TRUE       0. a     
  2    1. FALSE      3. b     
  3    3. TRUE       0. d     
  4    3. FALSE      7. a  
  </code></pre>
  
  <p>I would very much appreciate any help how to proceed from here. I tried to specify my question as precisely as possible, let me know if anything is unclear.</p>
  
  <hr>
  
  <p>Edit: Following @Henrik's suggestion, I will add some more complexity to my question. This dataset should work in this regard: </p>
  
  <pre><code>df &lt;- data.frame(c(1,1,1,2,2,2,3,3,3,4,4,4,4,4),c(TRUE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE,TRUE,
                                                    FALSE,TRUE,FALSE,TRUE,FALSE,TRUE), 
                   c(0,0,0,5,5,0,0,0,7,0,6,0,3,0), c("a","b","c","d","a","b","c","d","a","b",
                                                     "c","d","a","b"))
  colnames(df) &lt;- c("ID", "Status", "Number", "Letter")
  
  &gt; df
     ID Status Number Letter
  1   1   TRUE      0      a
  2   1   TRUE      0      b
  3   1   TRUE      0      c
  4   2  FALSE      5      d
  5   2  FALSE      5      a
  6   2   TRUE      0      b
  7   3   TRUE      0      c
  8   3   TRUE      0      d
  9   3  FALSE      7      a
  10  4   TRUE      0      b
  11  4  FALSE      6      c
  12  4   TRUE      0      d
  13  4  FALSE      3      a
  14  4   TRUE      0      b
  </code></pre>
  
  <p>Here is my proposed solution which, however, does not work for individual rows (cf. 12): </p>
  
  <pre><code>df2 &lt;- df %&gt;% 
    group_by(ID) %&gt;%
    mutate(Status2 = if (!all(Status)) replace(Status, cumsum(!Status) &lt; 1, FALSE) else TRUE,
           Number2 = if (!all(Status)) replace(Number, cumsum(!Status) &lt; 1,
                                                        first(Number[Status == FALSE])) 
                     else first(replace(Number, cumsum(!Status) &lt; 1, Number[Status == TRUE])))
  
  &gt; df2
  # A tibble: 14 x 6
  # Groups:   ID [4]
        ID Status Number Letter Status2 Number2
     &lt;dbl&gt; &lt;lgl&gt;   &lt;dbl&gt; &lt;fct&gt;  &lt;lgl&gt;     &lt;dbl&gt;
   1    1. TRUE       0. a      TRUE         0.
   2    1. TRUE       0. b      TRUE         0.
   3    1. TRUE       0. c      TRUE         0.
   4    2. FALSE      5. d      FALSE        5.
   5    2. FALSE      5. a      FALSE        5.
   6    2. TRUE       0. b      TRUE         0.
   7    3. TRUE       0. c      FALSE        7.
   8    3. TRUE       0. d      FALSE        7.
   9    3. FALSE      7. a      FALSE        7.
  10    4. TRUE       0. b      FALSE        6.
  11    4. FALSE      6. c      FALSE        6.
  12    4. TRUE       0. d      TRUE         0.
  13    4. FALSE      3. a      FALSE        3.
  14    4. TRUE       0. b      TRUE         0.
  </code></pre>
  
  <p>The result for Number2 in row 12 should correspond to the subsequent row, i.e. be 3.</p>