问题描述
我确信这个答案以前,但我找不到我的生活线程!我试图用r来产生数据框中xy坐标对之间所有距离的列表。数据存储如下:
ID = c('1','2','3','4 ','5','6','7')
x = c(1,2,4,5,1,3,1)
y = c(3,5,6,3, 1,5,1)
df = data.frame(ID,x,y)
现在我可以计算两点之间的距离:
$ p $ (y1-y2)^ 2)。
然而,我不确定下一步该去哪里。我应该使用plyr还是for循环?
感谢您的帮助!
您可以使用自连接来获取所有组合,然后应用您的距离公式。所有这些都可以使用 tidyverse
(Hadley Wickham的软件包组合)轻松实现:
#加载tidyverse
库(tidyverse)
#设置一个假钥匙(仅仅是一个常量)
df< - df %>%mutate(k = 1)
#执行连接,删除键,然后创建距离
df%>%
full_join(df,by = k)%>%
mutate(dist = sqrt((xx-xy)^ 2 +(yx - yy)^ 2))%>%
select(-k)
注意使用这种方法,您还将计算每个点与其自身之间的距离(以及所有其他点)。虽然很容易过滤掉这些点:$ b
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $'$' )%>%
filter(ID.x!= ID.y)%>%
mutate(dist = sqrt((xx - xy)^ 2 +(yx - yy)^ 2 ))%>%
select(-k)
有关使用 tidyverse
一套我推荐的或 tidyverse
。
I'm sure this has been answered before, but I can't find the thread for the life of me!
I am trying to use r to produce a list of all the distances between pairs of xy coordinates in a dataframe. The data is stored something like this:
ID = c('1','2','3','4','5','6','7')
x = c(1,2,4,5,1,3,1)
y = c(3,5,6,3,1,5,1)
df= data.frame(ID,x,y)
At the moment I can calculate the distance between two points using:
length = sqrt((x1 - x2)^2+(y1 - y2)^2).
However, I am uncertain as to where to go next. Should I use something from plyr or a for loop?
Thanks for any help!
You can use a self-join to get all combinations then apply your distance formula. All of this is easily do-able using the tidyverse
(combination of packages from Hadley Wickham):
# Load the tidyverse
library(tidyverse)
# Set up a fake key to join on (just a constant)
df <- df %>% mutate(k = 1)
# Perform the join, remove the key, then create the distance
df %>%
full_join(df, by = "k") %>%
mutate(dist = sqrt((x.x - x.y)^2 + (y.x - y.y)^2)) %>%
select(-k)
N.B. using this method, you'll also calculate the distance between each point and itself (as well as with all other points). It's easy to filter those points out though:
df %>%
full_join(df, by = "k") %>%
filter(ID.x != ID.y) %>%
mutate(dist = sqrt((x.x - x.y)^2 + (y.x - y.y)^2)) %>%
select(-k)
For more information about using the tidyverse
set of packages I'd recommend R for Data Science or the tidyverse
website.
这篇关于用于计算包含xy坐标列表的数据帧中所有点之间的距离的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!