I'm trying to find the 5 closest stations from one data set (set1
) to another data set (set2
). This post is what I'm using as a basis and it seems simple to find the single closest, but I'm writing for
loops to deal with it and is not efficient. Furthermore, I'm getting and error and don't understand why it's not working. Ideally, I would like to use set1
to find the closest stations in set2
, find the 5 closest stations, and add a column for each station, for each unique id from set1
.
Edit: This question is different from How to assign a name to lat-long observations based on shortest distance because I'm trying to find the 5 closest stations, not just a single distance. Also, the method is different for finding the minimum. Please reopen this question.
dput:
set1 <- structure(list(id = c(5984, 7495, 4752, 2654, 4578, 9865, 3265,
1252, 4679, 1346), lat = c(48.39167, 48.148056, 48.721111, 47.189167,
47.054443, 47.129166, 47.306667, 47.84, 47.304167, 48.109444),
lon = c(13.671114, 12.866947, 15.94223, 11.099736, 12.958342,
14.203892, 11.86389, 16.526674, 16.193064, 17.071392)), row.names = c(NA,
10L), class = "data.frame", .Names = c("id", "lat", "lon"))
set2 <- structure(list(id = 1:10, lat = structure(c(35.8499984741211,
34.75, 70.9329986572266, 78.25, 69.6829986572266, 74.515998840332,
70.3659973144531, 67.265998840332, 63.6990013122559, 60.1990013122559
), .Dim = 10L), lon = structure(c(14.4829998016357, 32.4000015258789,
-8.66600036621094, 15.4670000076294, 18.9160003662109, 19.0160007476807,
31.0990009307861, 14.3660001754761, 9.59899997711182, 11.0830001831055
), .Dim = 10L)), row.names = c(NA, 10L), class = "data.frame", .Names = c("id",
"lat", "lon"))
Code:
library(rgeos)
library(sp)
set1sp <- SpatialPoints(set1)
set2sp <- SpatialPoints(set2)
for (i in length(set1$id)){
for (j in 4:9){
if(i == 1) {
sub <- set2
set1[i,j] <- apply(gDistance(set1sp, set2sp, byid=TRUE), 1, which.min)
sub <- filter(sub, id != set1[i,j])}
else{
set1[i,j] <- apply(gDistance(set1sp, set2sp, byid=TRUE), 1, which.min)
sub <- filter(sub, id != set1[i,j])}
}
}
Output error:
Error in `[<-.data.frame`(`*tmp*`, i, j, value = c(8L, 8L, 8L, 8L, 8L, :
replacement has 10 rows, data has 1