title: Find positive values preceding local maxima
body: <p>I have a a data frame with values for a particular time span. I have located local maxima using function <a href="https://stackoverflow.com/questions/34205515/finding-local-maxima-and-minima-in-r?noredirect=1&amp;lq=1">find_peaks</a>. They are marked as <code>TRUE</code> in a column named <code>peak</code>: </p>
  
  <pre><code>test &lt;- 
  structure(list(year = 1996:2016, value = c(-0.5214506, -0.8037488, 
      0.1138524, 0.9939848, 1.7027944, 0.6448417, 0.1204489, -1.2254546, 
      -0.6733273, -0.7457323, 0.4874829, 2.2080809, 2.0609055, -2.5291374, 
      -1.5272201, 0.3057773, 0.1383523, -0.6455441, -0.8364883, -0.8907073, 
      -0.7940878), peak = c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, 
      FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, 
      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)), class = c("tbl_df", 
      "tbl", "data.frame"), row.names = c(NA, -21L))   
  
  test
  # A tibble: 21 x 3
      year  value peak 
     &lt;int&gt;  &lt;dbl&gt; &lt;lgl&gt;
   1  1996 -0.521 FALSE
   2  1997 -0.804 FALSE
   3  1998  0.114 FALSE
   4  1999  0.994 FALSE
   5  2000  1.70  TRUE 
   6  2001  0.645 FALSE
   7  2002  0.120 FALSE
   8  2003 -1.23  FALSE
   9  2004 -0.673 FALSE
  10  2005 -0.746 FALSE
  11  2006  0.487 FALSE
  12  2007  2.21  TRUE 
  13  2008  2.06  FALSE
  14  2009 -2.53  FALSE
  15  2010 -1.53  FALSE
  16  2011  0.306 FALSE
  17  2012  0.138 FALSE
  18  2013 -0.646 FALSE
  19  2014 -0.836 FALSE
  20  2015 -0.891 FALSE
  21  2016 -0.794 FALSE
  </code></pre>
  
  <p>I have to find consecutive non-negative values that precede located peaks (+ the peak). There are 2 peaks in this example, but there can be more. Result should look like: </p>
  
  <pre><code># A tibble: 5 x 3
     year value peak 
    &lt;int&gt; &lt;dbl&gt; &lt;lgl&gt;
  1  1998 0.114 FALSE
  2  1999 0.994 FALSE
  3  2000 1.70  TRUE 
  4  2006 0.487 FALSE
  5  2007 2.21  TRUE 
  </code></pre>
  
  <p>I tried some things, but I could not find the way to solve this. Any help would be appreciated.  </p>