title: Find the first minimum value in R?
body: <p>How can I find the first minimum value in a vector? E.g., in <code>y</code> I'd like to return <code>3</code> because it is the first time the values on either side of <code>y[3]</code> are greater than <code>y[3]</code>. I wrote a function to do this but wondering if there is an easier way? I also need to account for the case when the first value is less than the second value. E.g., in <code>z</code>.</p>
  <pre><code>y &lt;- c(2448,2442,2438,2440,2438,2444,2431,2433,2434)
  plot(y)
  
  getFirstMin &lt;- function(x){
    if(x[1] &lt; x[2]) res &lt;- 1
    else res &lt;- min(which(diff(x) &gt; 0))
    return(res)
  }
  getFirstMin(y)
  
  z &lt;- c(2408,2442,2438,2440,2438,2444,2431,2433,2434)
  plot(z)
  getFirstMin(z)
  </code></pre>