title: How to perform lag/lead over dataframe depending on the row index
body: <p>How do you lag/lead over dataframe depending on the value of the row - or the row index - to achieve the following output.</p>
  <p>Here is a sample dataset:</p>
  <pre><code>id &lt;- c(1,1,1,1,1,1)
  a &lt;- c(&quot;X1&quot;,&quot;Mar-20&quot;,&quot;X2&quot;,&quot;X3&quot;,&quot;Apr-20&quot;, &quot;X4&quot;)
  
  test &lt;- data.frame(id,a)
  </code></pre>
  <p>Desired output:</p>
  <pre><code>id|a
  1 |Mar-20
  1 |Mar-20
  1 |Mar-20
  1 |Apr-20
  1 |Apr-20
  1 |Apr-20
  </code></pre>