title: Subset and change values of grouped data frame
body: <p>I need to:</p>
  
  <ul>
  <li>Group by year</li>
  <li>If there is a value higher <code>threshold</code> within first five values of any column</li>
  <li>Change it to <code>threshold</code> value, else</li>
  <li>leave as is.
  I would need this to be applicable to data frame with many columns and groups. 
  <a href="https://stackoverflow.com/questions/20204257/subset-data-frame-based-on-number-of-rows-per-group">Somewhat similar question</a> but it is unfortunately beyond my R knowledge.   </li>
  </ul>
  
  <p>Data frame:</p>
  
  <pre><code>             year var1 var2  #Desired output:
          1     1   10    1    #change first five values in  var1 to twofor year 1
          2     1   11    1
          3     1   12    1
          4     1   13    1
          5     1   14    1
          6     1   15    1
          7     1   16    1
          8     1   17    1
          9     1   18    1
          10    1   19    1
          11    2   20    1     #change first five values in  var1 to 2 for year 2
          12    2   21    1     #var2 stays the same since it is below threshold
          13    2   22    1
          14    2   23    1
          15    2   24    1
          16    2   25    1
          17    2   26    1
          18    2   27    1
          19    2   28    1
          20    2   29    1
  </code></pre>
  
  <p>Data frame code: </p>
  
  <pre><code>threshold &lt;- 2        
  df &lt;- data.frame(year = c(rep(1, 10,), rep(2,10)),
                       var1 = seq(10,29, 1),
                       var2 = rep(1,20))
      df_out &lt;- data.frame(year = c(rep(1, 10,), rep(2,10)),
                       var1 = c(rep(2,5), seq(16,20, 1), rep(2,5),seq(26,30)),
                       var2 = rep(1,20))
  </code></pre>