title: Find closest value in a vector with binary search
body: <p>As a silly toy example, suppose</p>
  
  <pre><code>x=4.5
  w=c(1,2,4,6,7)
  </code></pre>
  
  <p>I wonder if there is a simple R function that finds the index of the closest match to <code>x</code> in <code>w</code>.  So if <code>foo</code> is that function, <code>foo(w,x)</code> would return <code>3</code>.  The function <code>match</code> is the right idea, but seems to apply only for exact matches.  </p>
  
  <p>Solutions <a href="http://r.789695.n4.nabble.com/Find-the-closest-value-in-a-list-or-matrix-td838131.html" rel="noreferrer">here</a> (e.g. <code>which.min(abs(w - x))</code>, <code>which(abs(w-x)==min(abs(w-x)))</code>, etc.) are all <code>O(n)</code> instead of <code>log(n)</code> (I'm assuming that <code>w</code> is already sorted).</p>