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

发布于 2025-02-09 15:10:17 字数 299 浏览 1 评论 0原文

我有一个数据框架,其中包含数千行关于房屋的行。对于每一行,都有两个列的经度和纬度。

我的目的是计算每个点到相同的固定点(例如距离市中心)的距离,以最终将列添加到我的数据框架中。

我尝试了软件包的地球圈,但是我无法自动化每行的计算。

CityCenter是固定点的经度和纬度的向量,我正在寻找应该看起来像的东西 dist(CityCenter,C(DF $经度,DF $ latitude))

因为第二个向量是列表,R不认为它是长度2,这是计算距离所必需的。

事先感谢您的帮助

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 (e.g. 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.

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))

Because the second vector is a list, R does not consider it as length 2, which is required to compute the distance.

Thanks in advance for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2025-02-16 15:10:17

c(DF $经度,DF $ latitude)只是一个长向量。

在这种情况下,您需要传递数据框架的列,

library(geosphere)    
distGeo((citycenter, df[, c("longitude", "latitude")])

现在dist()函数将计算城市中心位置之间的距离,而数据框中的每一行。

c(df$longitude,df$latitude) is just one long vector.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文