title: Test the last value dyplr::mutated for calculating the current value
body: <p>I am trying to mutate a new column in mtcars, but make the new entry (row) dependent in on the just-previous row in the same column (the example using mtcars is admittedly nonsense).</p>
  <p>Simplify mtcars with...</p>
  <pre><code>m.subset &lt;- mtcars[2:7, 10:11]  # top few rows and only the relevant columns
  </code></pre>
  <p>Similar questions have been asked...</p>
  <p><a href="https://stackoverflow.com/questions/56638199/reuse-value-of-previous-row-during-dplyrmutate">Reuse value of previous row during dplyr::mutate</a></p>
  <p><a href="https://stackoverflow.com/questions/66485682/use-the-diff-function-with-mutate-at-from-dplyr">use the diff function with mutate at from dplyr</a></p>
  <p>...and others, but all seem to refer to existing columns, not the subject one -- or they refer to multiple columns/rows as they currently exist.</p>
  <p>For example, neither of the following work:</p>
  <pre><code>mtcars1 &lt;- mtcars %&gt;% # doesn't work
    mutate (
      newcol = gear + carb + 
        ifelse(shift(newcol, n=1, type = &quot;lag&quot;) &gt;4, 0, 1)
    )
  
  mtcars1 &lt;- mtcars %&gt;% # doesn't work
    mutate (
      newcol = gear + carb + 
        ifelse(shift(., n=1, type = &quot;lag&quot;) &gt;4, 0, 1)
    )
  </code></pre>
  <p>...and while the following works, it's wrong:</p>
  <pre><code>mtcars1 &lt;- mtcars %&gt;% # works but wrong
    mutate (
      newcol = gear + carb + 
        ifelse(lag(.) &gt;4, 0, 1)
    )
  </code></pre>
  <p>Result I am looking for is:</p>
  <p><a href="https://i.stack.imgur.com/zyTIb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zyTIb.png" alt="enter image description here" /></a></p>