2016-12-25 1 views
2

R (첨부 된 그림에서 빨간색과 검은 색으로 표시됨)에 2 개의 별도의 공간 지점 데이터 프레임이 있습니다. 'red'데이터 세트의 데이터 속성을 R의 'black'데이터 세트의 가장 가까운 위치로 가져 오는 방법은 무엇입니까?R을 사용하여 좌표의 공간 데이터 세트를 병합하는 방법?

plot

+0

공간 데이터 세트에 관한 일반적인 R 프로그래밍 질문입니다. 답변은 모든 사람이 즐길 수 있도록 게시해야합니다. 다른 포럼에서이 질문에 대한 답을 찾을 수 없었습니다. –

답변

2

다음은 문제를 접근 할 수있는 한 가지 방법입니다.

library(raster) 
library(sp) 

### create some example datasets 
coords_A = cbind(runif(10, 1, 10), runif(10,1,10)) 
sp_A = SpatialPoints(coords_A) 
spdf_A = SpatialPointsDataFrame(coords_A, data.frame(varA=letters[1:10])) 

coords_B = cbind(runif(10, 1, 10), runif(10,1,10)) 
sp_B = SpatialPoints(coords_B) 
spdf_B = SpatialPointsDataFrame(coords_B, data.frame(varB=letters[11:20], varC=LETTERS[11:20])) 

### compute the complete distance matrix between the two sets of points 
dist_mat <- pointDistance(spdf_A, spdf_B, lonlat = FALSE, allpairs = TRUE) 

### identify nearest point in dataset B for every point in dataset A 
nearest <- apply(dist_mat, 1, which.min) 

### bind together the data from the dataset B (in your case the "red points") 
### at the closest point to dataset A ("black points") 
[email protected]<- cbind([email protected], [email protected][nearest,]) 
+0

귀하의 모의 사례가 매력처럼 작동했습니다, 감사합니다! –

관련 문제