title: R: find nearest index
body: <p>I have two vectors with a few thousand points, but generalized here:</p>
  
  <pre><code>A &lt;- c(10, 20, 30, 40, 50)
  b &lt;- c(13, 17, 20)
  </code></pre>
  
  <p>How can I get the indicies of <code>A</code> that are <em>nearest</em> to <code>b</code>? The expected outcome would be <code>c(1, 2, 2)</code>.</p>
  
  <p>I know that <code>findInterval</code> can only find the first occurrence, and not the nearest, and I'm aware that <code>which.min(abs(b[2] - A))</code> is getting warmer, but I can't figure out how to vectorize it to work with long vectors of both <code>A</code> and <code>b</code>.</p>