title: findInterval() with right-closed intervals
body: <p>The great <code>findInterval()</code> function in R uses left-closed sub-intervals in its <code>vec</code> argument, as shown in its docs:</p>
  
  <blockquote>
    <p>if <code>i &lt;- findInterval(x,v)</code>, we have <code>v[i[j]] &lt;= x[j] &lt; v[i[j] + 1]</code></p>
  </blockquote>
  
  <p>If I want right-closed sub-intervals, what are my options?  The best I've come up with is this:</p>
  
  <pre><code>findInterval.rightClosed &lt;- function(x, vec, ...) {
    fi &lt;- findInterval(x, vec, ...)
    fi - (x==vec[fi])
  }
  </code></pre>
  
  <p>Another one also works:</p>
  
  <pre><code>findInterval.rightClosed2 &lt;- function(x, vec, ...) {
    length(vec) - findInterval(-x, -rev(vec), ...)
  }
  </code></pre>
  
  <p>Here's a little test:</p>
  
  <pre><code>x &lt;- c(3, 6, 7, 7, 29, 37, 52)
  vec &lt;- c(2, 5, 6, 35)
  findInterval(x, vec)
  # [1] 1 3 3 3 3 4 4
  findInterval.rightClosed(x, vec)
  # [1] 1 2 3 3 3 4 4
  findInterval.rightClosed2(x, vec)
  # [1] 1 2 3 3 3 4 4
  </code></pre>
  
  <p>But I'd like to see any other solutions if there's a better one.  By "better", I mean "somehow more satisfying" or "doesn't feel like a kludge" or maybe even "more efficient". =)</p>
  
  <p>(Note that there's a <code>rightmost.closed</code> argument to <code>findInterval()</code>, but it's different - it only refers to the final sub-interval and has a different meaning.)</p>