# Extracting values of one point dataset to second point dataset based on proximity using R or ArcGIS Desktop?

I have to point shapefiles and would like to extract values from one point shapefile to another one based on some measure of proximity, i.e. for any given point in dataset x, extract the mean value for all points in dataset y within a 10km radius of the given point in dataset x. Or, for any given point in x, extract the value of the closest point in y and add to x.

I'm familiar with extract function in r to extract raster values to point. Not sure if sample data would help in this one, but would like to do this in r, or failing that, ArcGIS Desktop.

This ESRI solution is a little involved. Kind of surprised I can't find something more direct. At any rate:

1) Generate a table of near points. Can specify a distance limit.

2) JoinField to attach original shapefile's attributes to the nearpoints result

3) Statistics on that, using the feature id as the case_item asking for the mean (or whatever stat you want) of the attribute in question.

4) Copy the original shapefile to a new one

3) Joinfield to attach the results of the statistics to the copied set of points.

• Thank you for your response Roland. I was aware of the near point method, but your more detailed explanation is helpful. Does anyone have advice for using R to do something similar? Feb 4, 2014 at 2:23
• `R` will be ok for smallish datasets (fewer than about 10^8 point pairs, roughly) and hopelessly inadequate for larger datasets because you have to resort to solutions that compute all point-to-point distances. A good GIS will build "spatial indexes" that hugely facilitate proximity calculations. The typical improvement is from a O(mn) algorithm (m and n are the sizes of the two datasets) to a O((m+n)*log(n)) algorithm. There *may be `R` packages that implement more efficient algorithms in C or Fortran. Feb 4, 2014 at 14:46
• The python package, `pandas`, might be more efficient, but this does seem like a "bread-and-butter" GIS operation as ~whuber points out. Feb 4, 2014 at 16:09

R solution is rather straightforward:

``````# needs rgeos library
library(rgeos)

### create sample data
df1 <- data.frame(x=runif(100), y=runif(100), id=1:100)
df2 <- data.frame(x=runif(200), y=runif(200), value=runif(200,min=10,max=100))

coordinates(df1) <- ~x+y
coordinates(df2) <- ~x+y

### value of closest point:
# find closest point in second layer
closest <- apply(gDistance(df1, df2, byid=T), MARGIN=2, FUN=which.min)
# set the value
df1\$closest_value <- df2\$value[closest]