2016-12-25 5 views
2

私は、Rに2つの別々の空間点データフレームを持っています(添付されたプロットの赤と黒の色付き)。 「赤」データセットからRの「黒」データセットの最も近い場所にデータ属性をインポートする方法は?ここで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

あなたのモックの例は、魅力のように働いた、ありがとう! –

関連する問題