在R中获得距离的最佳方法是什么?

hjzp0vay  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(102)

我希望记录从一个县的SUD中心到医院的距离。代码如下:

alameda_distances <- st_distance(alameda_SUDs_centers, alameda_mediCal_hospitals) * 0.000621371
alameda_mediCal_hospitals$distance <- (as.vector(apply(alameda_distances,1, min)))

但是我在这行代码之后收到了这个错误:

alameda_mediCal_hospitals$distance <- (as.vector(apply(alameda_distances,1, min)))

Error in `[[<-.data.frame`(`*tmp*`, i, value = c(0.452148118348417, 1.15003687022296, :
replacement has 32 rows, data has 11

有什么建议吗?我在RMarkdown中使用这些库:

library(janitor)
library(sf)
library(spData)
library(sp)
library(raster)

我尝试将“NA”添加到新变量阿拉米达_mediCal_hospitals$distance

anauzrmj

anauzrmj1#

我用这个:

library(geosphere)
distm(c(long1, lat1), c(long2, lat2 ), fun = distHaversine)

它返回以米为单位的距离。

相关问题