R - Fastest way to find nearest value in vector : <p>I have two integer/posixct vectors:</p>

<pre><code>a &lt;- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) #has &gt; 2 mil elements
b &lt;- c(4,6,10,16) # 200000 elements
</code></pre>

<p>Now my resulting vector c should contain for each element of vector a the nearest element of b:</p>

<pre><code>c &lt;- c(4,4,4,4,4,6,6,...)
</code></pre>

<p>I tried it with apply and which.min(abs(a-b)) but it's very very slow.</p>

<p>Is there any more clever way to solve this? Is there a data.table solution?</p>
