简体   繁体   English

如何在 R Studio 中计算点与固定位置之间的距离

[英]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.

相关问题 如何计算带有正负坐标的R中的点之间的距离 - How to calculate distance between points in R with negative and positive coordinate R-如何计算两组坐标点之间的距离? - R - How to calculate distance between two sets of coordinate points? 如何计算R中沿线的两个点之间的地理距离? - How to calculate geographic distance between two points along a line in R? 如何使用R计算点与参考点之间的距离? - How to Calculate Distance between points from a reference point using R? 如何使用 leaflet、geosphere 和 R shiny 计算 2 点之间的距离 - How to calculate distance between 2 points with leaflet, geosphere, and R shiny 如何使用 R 计算两组点(坐标)之间的欧氏距离的房屋距离 - How to calculate House distance with euclidean distance between two set of points (coordinates) with R 如何计算地理位置和边界之间的距离? - How to calculate distance between geographic points and boundaries? 如何计算R中缓冲距离内点到多边形的距离 - How to calculate distance from points to multipolygons within a buffer distance in R R:计算每组连续点之间的距离并将其分组 - R: Calculate distance between consecutive points per group and group them R - 计算沿折线的两点之间的距离 - R - Calculate distance between two points along a polyline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM