title: How to create a new column with values from previous row in exsisting column, in a tibble, in R
body: <p>I want to create a new column, that consists of the last value from a previous period for the same ID, placed in the same row as the first value for the next period. If there is no previous period NA should be applied.</p>
  <p>However, I can't find any functions in any packages to solve this issue for me, so I expect I have to write a loop?</p>
  <p>Does anyone out there have any idea how to solve this in a tidy manner (with or without a loop), that can be applied to a big tibble (+4 million observations)?</p>
  <p>My data is ordered like the following df, and the goal is df1:</p>
  <pre><code>df &lt;- tibble(
    ID = rep(c(77,88,99),each=6),
    PERIOD = rep(c(1,2,3,1,2,3,1,2,3),each=2),
    DATE = seq(as.Date(&quot;2020-06-01&quot;), as.Date(&quot;2020-06-18&quot;), by= &quot;days&quot;),
    RESULT = seq(from = 10, to = 44, by = 2)
  )
  df
  # A tibble: 18 x 4
        ID PERIOD DATE       RESULT
     &lt;dbl&gt;  &lt;dbl&gt; &lt;date&gt;      &lt;dbl&gt;
   1    77      1 2020-06-01     10
   2    77      1 2020-06-02     12
   3    77      2 2020-06-03     14
   4    77      2 2020-06-04     16
   5    77      3 2020-06-05     18
   6    77      3 2020-06-06     20
   7    88      1 2020-06-07     22
   8    88      1 2020-06-08     24
   9    88      2 2020-06-09     26
  10    88      2 2020-06-10     28
  11    88      3 2020-06-11     30
  12    88      3 2020-06-12     32
  13    99      1 2020-06-13     34
  14    99      1 2020-06-14     36
  15    99      2 2020-06-15     38
  16    99      2 2020-06-16     40
  17    99      3 2020-06-17     42
  18    99      3 2020-06-18     44
  
  df1 &lt;- tibble(
    ID = rep(c(77,88,99),each=6),
    PERIOD = rep(c(1,2,3,1,2,3,1,2,3),each=2),
    DATE = seq(as.Date(&quot;2020-06-01&quot;), as.Date(&quot;2020-06-18&quot;), by= &quot;days&quot;),
    RESULT = seq(from = 10, to = 44, by = 2),
    RESULT_post = c(&quot;NA&quot;,&quot;NA&quot;,12,&quot;NA&quot;,16,&quot;NA&quot;,&quot;NA&quot;,&quot;NA&quot;,24,&quot;NA&quot;,28, 
                    &quot;NA&quot;,&quot;NA&quot;, &quot;NA&quot;,36, &quot;NA&quot;,40, &quot;NA&quot; )
  )
  df1
  
  # A tibble: 18 x 5
        ID PERIOD DATE       RESULT RESULT_pre
     &lt;dbl&gt;  &lt;dbl&gt; &lt;date&gt;      &lt;dbl&gt; &lt;chr&gt;     
   1    77      1 2020-06-01     10 NA        
   2    77      1 2020-06-02     12 NA        
   3    77      2 2020-06-03     14 12        
   4    77      2 2020-06-04     16 NA        
   5    77      3 2020-06-05     18 16        
   6    77      3 2020-06-06     20 NA        
   7    88      1 2020-06-07     22 NA        
   8    88      1 2020-06-08     24 NA        
   9    88      2 2020-06-09     26 24        
  10    88      2 2020-06-10     28 NA        
  11    88      3 2020-06-11     30 28        
  12    88      3 2020-06-12     32 NA        
  13    99      1 2020-06-13     34 NA        
  14    99      1 2020-06-14     36 NA        
  15    99      2 2020-06-15     38 36        
  16    99      2 2020-06-16     40 NA        
  17    99      3 2020-06-17     42 40        
  18    99      3 2020-06-18     44 NA
  </code></pre>
  <p>All inputs are appreciated</p>
  <p>Thx / Sophia</p>