我想使用sp package
找到两个向量数据帧的最近点location <- c('RODELILLO','RAPEL','QUINTA NORMAL')
lat <-c(-33.03711,-33.93225,-33.43165)
lon <- c(-71.58291,-71.73317,-70.68254)
est <- data.frame(location,lat,lon)
id <- c(1,5,8)
lat2 <- c(-32.59830, -33.54318, -33.54318)
lon2 <- c(-72.034, -71.096, -70.158)
pred <- data.frame(id,lat2,lon2)
est.sp <- SpatialPoints(list(est$lat,est$lon))
pred.sp <- SpatialPoints(list(pred$lat,pred$lon))
我尝试使用sp包的spDists函数
est$near <- apply(spDists(est.sp,pred.sp, longlat = TRUE), 1, which.min)
但它返回错误
>In spDists(est.sp, pred.sp, longlat = TRUE) :
spDists: argument longlat conflicts with CRS(x); using the value TRUE
当我删除longlat参数时,它可以正常工作
est$near <- apply(spDists(est.sp,pred.sp, longlat = TRUE), 1, which.min)
> location lat lon near
1 RODELILLO -33.03711 -71.58291 1
2 RAPEL -33.93225 -71.73317 2
3 QUINTA NORMAL -33.43165 -70.68254 2
但是我不知道它是否正常工作,将我的观点视为纬度经度对。我可以提供一些建议吗?感谢