How to extract values of one point dataset to second point dataset based on proximity using R?

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. Your help and patience are greatly appreciated!

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]

### mean value in radius
mv <- rep(NA, length(df1))
# for every point, create buffer and calculate mean from points inside the buffer
for (i in 1:length(df1)){
bf <- gBuffer(df1[i,], width=0.1)
mv[i] <- mean(df2$value[gCoveredBy(df2, bf, byid=T)]) } # set values df1$mean_near <- mv
• What packages are you using? – Aaron Feb 4 '14 at 13:08
• sp for handling spatial data and rgeos for geoprocessing – Zbynek Feb 4 '14 at 13:16

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? – RyanM Feb 4 '14 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. – whuber Feb 4 '14 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. – Roland Feb 4 '14 at 16:09