[R-sig-Geo] Finding nearest point

Roger Bivand Roger.Bivand at nhh.no
Thu Apr 26 22:50:44 CEST 2007


On Thu, 26 Apr 2007, bertrand toupin wrote:

> Hi, I have 2 sets of data points. One is a grid, consisting of 87
> points.  The other is 300 meteorological stations.
> 
> I'd like to chose 10 of the grid points and I'd like to have the nearest
> station for each of my 10 grid-points.

If you have the coordinates of the chosen grid points (10 by 2 matrix) 
then something like lapply(1:10, function(i) spDistsN1(met_coords, 
grd[i,])) will return a 10-long list of 300-long vectors. Then 
sapply(res, function(x) order(x, ...)[1:10]) would get the distances and a 
more complex throw-away function using which() would say which they are. 
These are not checked, but look at the spDistsN1 help page in the sp 
package. It supports geographical coordinates too.

Roger

> 
> Is there a function that does it?
> 
> I have R 2.4.0, with gstat and rgdal installed.
> 
> Thanks a lot!
> 
>        
> ---------------------------------
> 
> ---------------------------------
> Get a sneak peak at messages with a handy reading pane.
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list