title: How to use dplyr to edit subset of rows based on relative location?
body: <p>I have election data where two of the elections fail to differentiate between the contest for Treasurer at the state level and the contest for County Treasurer. Data is presented like so:</p>
  <pre><code>&gt; elections
  # A tibble: 13 x 6
      year election contest   data              party number
     &lt;dbl&gt; &lt;chr&gt;    &lt;chr&gt;     &lt;chr&gt;             &lt;chr&gt;  &lt;dbl&gt;
   1  2010 general  Treasurer Registered Voters NP       628
   2  2010 general  Treasurer Times Counted     NP       320
   3  2010 general  Treasurer Times Blank Voted NP         4
   4  2010 general  Treasurer Times Over Voted  NP         0
   5  2010 general  Treasurer Scott K. Summers  G         15
   6  2010 general  Treasurer Dan Rutherford    R        245
   7  2010 general  Treasurer Robin Kelly       D         51
   8  2010 general  Treasurer James Pauly       Lib        5
   9  2010 general  Treasurer Registered Voters NP       628
  10  2010 general  Treasurer Times Counted     NP       320
  11  2010 general  Treasurer Times Blank Voted NP        38
  12  2010 general  Treasurer Times Over Voted  NP         0
  13  2010 general  Treasurer Rebecca C. McNeil R        282
  </code></pre>
  <p>Lines 9-13 should be &quot;County Treasurer,&quot; as most years are. The data is by precinct so manually editing the data would be a pain.</p>
  <p>Both of the years that have this issue, Rebecca C. McNeil is the only candidate, so it's easy to identify where the rows I need to change are and use <code>mutate</code> to fix the contest associated with the candidate.</p>
  <pre><code>library(dplyr)
  
  &gt; elections %&gt;% 
  +   mutate(contest = case_when(
  +     str_detect(data, &quot;Rebecca C&quot;) ~ &quot;County Treasurer&quot;,
  +     TRUE ~ contest))
  
  # A tibble: 13 x 6
      year election contest          data              party number
     &lt;dbl&gt; &lt;chr&gt;    &lt;chr&gt;            &lt;chr&gt;             &lt;chr&gt;  &lt;dbl&gt;
   1  2010 general  Treasurer        Registered Voters NP       628
   2  2010 general  Treasurer        Times Counted     NP       320
   3  2010 general  Treasurer        Times Blank Voted NP         4
   4  2010 general  Treasurer        Times Over Voted  NP         0
   5  2010 general  Treasurer        Scott K. Summers  G         15
   6  2010 general  Treasurer        Dan Rutherford    R        245
   7  2010 general  Treasurer        Robin Kelly       D         51
   8  2010 general  Treasurer        James Pauly       Lib        5
   9  2010 general  Treasurer        Registered Voters NP       628
  10  2010 general  Treasurer        Times Counted     NP       320
  11  2010 general  Treasurer        Times Blank Voted NP        38
  12  2010 general  Treasurer        Times Over Voted  NP         0
  13  2010 general  County Treasurer Rebecca C. McNeil R        282
  </code></pre>
  <p>But I need the 4 preceding rows to also have &quot;County Treasurer&quot; for the contest, with desired output as:</p>
  <pre><code># A tibble: 13 x 6
      year election contest          data              party number
     &lt;dbl&gt; &lt;chr&gt;    &lt;chr&gt;            &lt;chr&gt;             &lt;chr&gt;  &lt;dbl&gt;
   1  2010 general  Treasurer        Registered Voters NP       628
   2  2010 general  Treasurer        Times Counted     NP       320
   3  2010 general  Treasurer        Times Blank Voted NP         4
   4  2010 general  Treasurer        Times Over Voted  NP         0
   5  2010 general  Treasurer        Scott K. Summers  G         15
   6  2010 general  Treasurer        Dan Rutherford    R        245
   7  2010 general  Treasurer        Robin Kelly       D         51
   8  2010 general  Treasurer        James Pauly       Lib        5
   9  2010 general  County Treasurer Registered Voters NP       628
  10  2010 general  County Treasurer Times Counted     NP       320
  11  2010 general  County Treasurer Times Blank Voted NP        38
  12  2010 general  County Treasurer Times Over Voted  NP         0
  13  2010 general  County Treasurer Rebecca C. McNeil R        282
  </code></pre>
  <p>I've been thinking I could possibly do this through adding row numbers and using them for identification but I haven't figured out how to make that work. Any help would be appreciated.</p>