title: If condition is met, go 6 rows above and select the value in R
body: <p><a href="https://i.stack.imgur.com/lIlmt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lIlmt.png" alt="data sample" /></a></p>
  <p>I am trying to do in R the following: In my dataset (md2, attached), if L_ID=1, go 6 rows above and create a new dataframe with the variable Hway. I use the following command:</p>
  <pre><code>spc &lt;- data.frame(md$Hdwy[c(md2$L_ID[-6], 0) == 1])
  </code></pre>
  <p>Expecting to see:</p>
  <pre><code>&gt; spc
  44.46 40.00
  </code></pre>
  <p>It seems to work, however when I checked I noticed that it selects the value 1 row above instead of 6:</p>
  <pre><code>&gt; spc
      40.1 40.00
  </code></pre>
  <p>Any ideas?</p>