如何计算R Studio中点和固定位置之间的距离
我有一个数据框架,其中包含数千行关于房屋的行。对于每一行,都有两个列的经度和纬度。
我的目的是计算每个点到相同的固定点(例如距离市中心)的距离,以最终将列添加到我的数据框架中。
我尝试了软件包的地球圈,但是我无法自动化每行的计算。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
c(DF $经度,DF $ 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
Now the dist() function will compute the distance between the city center location with every row in the data frame.