title: Finding closest coordinates between two datasets R
body: <p>I have the two following datasets :</p>
  <pre class="lang-r prettyprint-override"><code>            ID             latitude         longitude
     1:      11001           -4.356            15.33
     2:      11002           -4.355            15.36
     3:      11003           -4.360            15.33
     4:      11004           -4.363            15.33
     5:      11005           -4.367            15.33
    ---                                             
  1977:      93085           -6.140            24.52
  1978:      93086           -6.568            24.24
  1979:      93087           -6.651            23.40
  </code></pre>
  <pre class="lang-r prettyprint-override"><code>          lon             lat
  1   14.2635707899353    -1.72819041855475
  2   14.6252329955268    -4.15798027488536
  3   16.3987113877547    -3.45049484822612
  4   17.4593940761681    -3.49348376028095
  5   17.5650287350763    -3.76706898554739
    --- 
  385 20.2021915511742    -9.11098960107533
  386 17.5816739683535    -8.34902586674811
  387 21.9970345436927    -5.38327274344268
  </code></pre>
  <p>I would like to find for each coordinates in dataset 1 its nearest coordinates in dataset2.</p>
  <p>I have tried the following code :</p>
  <pre class="lang-r prettyprint-override"><code>library(raster)
  d &lt;- pointDistance(data1[,3:2],data2[,1:2], lonlat=TRUE, allpairs=T)
  i &lt;- apply(d, 1, which.min)
  
  x$SRC_ID = y$SRC_ID[i]
  x$distance = d[cbind(1:nrow(d), i)]
  x
  </code></pre>
  <p>However it gave me this error :</p>
  <pre class="lang-r prettyprint-override"><code>Error in .geodist(x[i, 1], x[i, 2], y[, 1], y[, 2]) : 
    Not compatible with the requested type: [type=character; target=double].
  </code></pre>
  <p>Thanks for your help !</p>