title: Find the position of first negative number in a R vector that may be entirely positive
body: <p>Let's say I have a vector drawn from a normal distribution.</p>
  
  <pre><code>RandomVector&lt;-rnorm(10,mean=1,sd=1)
  &gt; RandomVector
  [1]  1.18186018  0.53810223  0.33888370  0.46593762  2.33963330 -0.08779368     -0.65178144 -0.61384231 -0.74351425
  [10] -0.51577616
  </code></pre>
  
  <p>I want to find the <strong>position</strong> of the first number &lt;=0 in the vector. The simplest way would be to use which.</p>
  
  <pre><code>FirstNegative&lt;-min(which(RandomVector&lt;=0))
  </code></pre>
  
  <p>However, in some cases the vector may have no negative numbers, in which case which( ) will return a vector of length zero. </p>
  
  <pre><code>&gt; RandomVector
   [1] 1.20150245 0.58668248 1.65633049 1.63277953 0.51134272 0.76385987 0.89085391 0.06504819 0.18011920 0.02400666
  </code></pre>
  
  <p>This will give you an error.</p>
  
  <pre><code>&gt; FirstNegative&lt;-min(which(RandomVector&lt;0))
  Warning message:
  In min(which(RandomVector &lt; 0)) :
    no non-missing arguments to min; returning Inf
  </code></pre>
  
  <p>Here are some solutions to this problem that I've come up with. All of them work, but I want to avoid an if/else statement if possible. Can anyone think of a more concise way?</p>
  
  <pre><code>for (j in 1:length(RandomVector)) {
          if (RandomVector[j]&lt;=0) {
              FirstNegative&lt;-j
              break;
              }
  </code></pre>
  
  <p>Alternatively...</p>
  
  <pre><code>WhichNegative&lt;-which(RandomVector&lt;=0)
  if (length(WhichNegative)&gt;0) {
      FirstNegative&lt;-min(WhichNegative)
      }
  </code></pre>
  
  <p>Same basic idea, but still using an if statement</p>
  
  <pre><code> if (any(RandomVector&lt;=0)==TRUE) {
       FirstNegative&lt;-min(which(RandomVector&lt;=0))
       }
  </code></pre>
  
  <p>The reason I'm asking is because this check will be made for a large number of vectors of long length. Plus I'm just curious.</p>