[英]How to calculate the distance between points and a fixed location in R Studio
I have a data frame containing thousands of rows about houses.我有一个包含数千行关于房屋的数据框。 For each row, there are two columns with the longitude and the latitude of this geographical point.
对于每一行,有两列包含该地理点的经度和纬度。
My aim is to calculate the distance of each point to the same fixed point (eg distance to city center) to eventually add the column to my data frame.我的目标是计算每个点到同一固定点的距离(例如到市中心的距离),以最终将列添加到我的数据框中。
I tried with the package geosphere, but I am unable to automatize the computation for each row.我尝试使用包 geosphere,但我无法自动化每一行的计算。
citycenter being the vector with the longitude and latitude of the fixed point, I'm looking for something that should look like dist(citycenter,c(df$longitude,df$latitude)) citycenter 是具有固定点经度和纬度的向量,我正在寻找应该看起来像dist(citycenter,c(df$longitude,df$latitude))的东西
Because the second vector is a list, R does not consider it as length 2, which is required to compute the distance.因为第二个向量是一个列表,所以 R 不认为它是长度 2,这是计算距离所必需的。
Thanks in advance for your help在此先感谢您的帮助
c(df$longitude,df$latitude)
is just one long vector. c(df$longitude,df$latitude)
只是一个长向量。
In this case you need to pass the columns of the data frame在这种情况下,您需要传递数据框的列
library(geosphere)
distGeo((citycenter, df[, c("longitude", "latitude")])
Now the dist() function will compute the distance between the city center location with every row in the data frame.现在 dist() 函数将计算城市中心位置与数据框中每一行之间的距离。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.