title: Finding the largest number in a vector which is smaller than specific value
body: <p>I want to find the largest grade of the failed subjects for students in R. I have written this function which works fine but it looks more like java code than R as it is traversing through vectors using loops instead of using special functions for that purpose in R. would you please suggest an alternative solution</p>
  
  <pre><code>grades &lt;- c(62, 100, 45, 40, 46, 55, 56, 70)
  
  largestFail &lt;- function(grades){
    location &lt;- -1
    faildFound &lt;- FALSE
  
    for(i in 1:length(grades)){
      if(grades[i] &lt; 50){
        if(!faildFound) location &lt;- i
        if(grades[i] &gt; grades[location]) location &lt;- i
        faildFound &lt;- TRUE
      }
    }
    return (location)
  }
  print (grades) # 5
  </code></pre>