[英]Calculate distance between multiple latitude and longitude points
I have a dataset that has latitude and longitude information for participants' home and work, and I'd like to create a new column in the dataset containing the euclidean distance between home and work for each participant.我有一个数据集,其中包含参与者家庭和工作地点的经纬度信息,我想在数据集中创建一个新列,其中包含每个参与者家庭和工作地点之间的欧氏距离。 I think this should be relatively simple, but all the other Q&As I've seen seem to be dealing with slightly different issues.我认为这应该相对简单,但我看到的所有其他问答似乎都在处理略有不同的问题。
To start, I tried running this code (using the geosphere package):首先,我尝试运行这段代码(使用 geosphere 包):
distm(c(homelong, homelat), c(worklong, worklat), fun=distHaversine)
But got an error saying "Error in.pointsToMatrix(x): Wrong length for a vector, should be 2" because (if I understand correctly) I'm trying to calculate the distance between multiple sets of two points.但是出现错误提示“Error in.pointsToMatrix(x): Wrong length for a vector, should be 2”,因为(如果我理解正确的话)我正在尝试计算多组两点之间的距离。
Can I adjust this code to get what I'm looking for, or is there something else I should be trying instead?我可以调整此代码以获得我正在寻找的东西,还是我应该尝试其他东西? Thanks!谢谢!
distm()
returns a distance matrix, which is not what you want; distm()
返回一个距离矩阵,这不是你想要的; you want the pairwise distances.你想要成对的距离。 So use the distance function ( distHaversine()
, distGeo()
, or whatever) directly:因此,直接使用距离 function( distHaversine()
、 distGeo()
或其他):
library(tidyverse)
locations <- tibble(
homelong = c(0, 2),
homelat = c(2, 5),
worklong = c(70, 60),
worklat = c(45, 60)
)
locations <- locations %>%
mutate(
dist = geosphere::distHaversine(cbind(homelong, homelat), cbind(worklong, worklat))
)
locations
#> # A tibble: 2 × 5
#> homelong homelat worklong worklat dist
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0 2 70 45 8299015.
#> 2 2 5 60 60 7809933.
Note that geosphere functions want matrices as inputs, so you can cbind()
your columns together.请注意,geosphere 函数需要矩阵作为输入,因此您可以将列一起cbind()
。 Don't c()
them;不要c()
他们; that's creating a single shapeless vector and losing the differentiation between lon and lat.那是在创建一个单一的无形矢量并失去经度和纬度之间的区别。 This is the cause of the error, I suspect;我怀疑这是错误的原因; the vector only has one dimension, not two like a matrix.向量只有一维,而不是像矩阵那样的二维。
You can have the latitudes and longitudes in a dataframe and then do rowwise operations on the dataframe to get the distance corresponding to each row.您可以将经纬度放在 dataframe 中,然后对 dataframe 进行按行运算,以获得每一行对应的距离。
library(tidyverse)
library(geosphere)
locations <- tibble(
homelong = c(0, 2),
homelat = c(2, 5),
worklong = c(70, 60),
worklat = c(45, 60)
)
locations %>%
rowwise() %>%
mutate(d = as.numeric(distm(c(homelong, homelat), c(worklong, worklat), fun = distHaversine)))
results in结果是
# A tibble: 2 x 5
# Rowwise:
homelong homelat worklong worklat d
<dbl> <dbl> <dbl> <dbl> <dbl>
1 0 2 70 45 8299015.
2 2 5 60 60 7809933.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.