title: Retaining the previous row value of currently mutated variable in dplyr
body: <p>Below is my dataset and I'm trying to use the previous row value of my currently mutated value in dplyr. first(start_time) wouldn't work because there's a group_by happening before this code hence the first refers to the first one in group which isn't what I want. </p>
  
  <p>This is the code that I am trying:</p>
  
  <pre><code>mutate(shift_start_time = if_else(diff==0 | diff &gt; 21600,start_time,lag(shift_start_time)))
  </code></pre>
  
  <p>But <code>shift_start_time</code> being the currently mutated variable isn't available for operation and I'm struggling for any other work around. </p>
  
  <p>Current:</p>
  
  <pre><code># A tibble: 703 × 4
              start_time            end_time      diff    shift_start_time
                  &lt;dttm&gt;              &lt;dttm&gt;    &lt;time&gt;              &lt;dttm&gt;
  1  2017-05-01 14:21:00 2017-05-01 14:42:18    0 secs 2017-05-01 14:21:00
  2  2017-05-01 14:48:00 2017-05-01 15:08:17  342 secs 1970-01-01 00:00:00
  3  2017-05-01 15:14:00 2017-05-01 15:35:31  343 secs 1970-01-01 00:00:00
  4  2017-05-01 15:42:00 2017-05-01 15:45:36  389 secs 1970-01-01 00:00:00
  5  2017-05-01 15:50:00 2017-05-01 16:51:47  264 secs 1970-01-01 00:00:00
  6  2017-05-01 16:39:00 2017-05-01 16:51:22 -767 secs 1970-01-01 00:00:00
  7  2017-05-01 17:37:00 2017-05-01 17:51:57 2738 secs 1970-01-01 00:00:00
  8  2017-05-01 17:46:00 2017-05-01 17:58:25 -357 secs 1970-01-01 00:00:00
  9  2017-05-01 17:59:00 2017-05-01 18:11:16   35 secs 1970-01-01 00:00:00
  10 2017-05-01 18:08:00 2017-05-01 18:22:22 -196 secs 1970-01-01 00:00:00
  </code></pre>
  
  <p>Required:</p>
  
  <pre><code># A tibble: 703 × 4
              start_time            end_time      diff    shift_start_time
                  &lt;dttm&gt;              &lt;dttm&gt;    &lt;time&gt;              &lt;dttm&gt;
  1  2017-05-01 14:21:00 2017-05-01 14:42:18    0 secs 2017-05-01 14:21:00
  2  2017-05-01 14:48:00 2017-05-01 15:08:17  342 secs 2017-05-01 14:21:00
  3  2017-05-01 15:14:00 2017-05-01 15:35:31  343 secs 2017-05-01 14:21:00
  4  2017-05-01 15:42:00 2017-05-01 15:45:36  389 secs 2017-05-01 14:21:00
  5  2017-05-01 15:50:00 2017-05-01 16:51:47  264 secs 2017-05-01 14:21:00
  6  2017-05-01 16:39:00 2017-05-01 16:51:22 -767 secs 2017-05-01 14:21:00
  7  2017-05-01 17:37:00 2017-05-01 17:51:57 2738 secs 2017-05-01 14:21:00
  8  2017-05-01 17:46:00 2017-05-01 17:58:25 -357 secs 2017-05-01 14:21:00
  9  2017-05-01 17:59:00 2017-05-01 18:11:16   35 secs 2017-05-01 14:21:00
  10 2017-05-01 18:08:00 2017-05-01 18:22:22 -196 secs 2017-05-01 14:21:00
  </code></pre>