两组点之间的距离 [英] Distance between two sets of points
问题描述
因此,在查看了此处关于stackoverflow的各种问题之后,我仍然无法将头缠绕在R中的dist函数上,或者一般来说甚至是距离矩阵。
So after looking at various Questions asked here on stackoverflow I'm still not able to wrap my head around the dist function in R or maybe even a distance matrix in general.
所以我有两个带有xy坐标的数据框。
So I have two dataframes with xy-coordinates.
df1 <- data.frame(x = runif(3,0,50), y = runif(3,0,50))
df2 <- data.frame(x = runif(20,0,50), y = runif(20,0,50))
我想遍历第一个数据框,并且想检查每个点/行的距离到第二个df中的所有点
这可能非常容易做到,很抱歉让我问这个问题,但我已经尝试了几个小时。
I want to iterate through the first dataframe and for each point/row I want to check the distance to all the points in the second df. This is probably extremely easy to do and I am sorry and ashamed that I am asking this, but I'm trying for hours now. Any help is appreciated!
推荐答案
pakcage proxy
可让您计算距离
the pakcage proxy
lets you calcualte distances between rows of matrices in addition to providing many more distance metrics.
library(proxy)
df1=data.frame(x=runif(3,0,50),y=runif(3,0,50))
df2=data.frame(x=runif(20,0,50),y=runif(20,0,50))
dist(df1, df2, method = "euclidean")
# [,1] [,2] [,3]
# [1,] 14.502244 15.272563 30.266583
# [2,] 33.152003 44.574128 32.464425
# [3,] 30.112443 42.935810 12.935461
# [4,] 31.749340 38.809452 45.214138
# [5,] 22.712121 25.358099 45.593262
# [6,] 24.702575 31.332886 41.528598
# [7,] 38.244377 46.768200 46.222620
# [8,] 13.686492 1.378374 41.466495
# [9,] 30.884628 40.148856 39.286827
#[10,] 7.615178 19.138047 27.597653
#[11,] 24.898710 22.577377 51.314388
#[12,] 27.962082 39.389227 29.863587
#[13,] 26.927504 39.687474 18.898984
#[14,] 22.249277 26.451882 43.537306
#[15,] 14.632805 24.817799 30.319079
#[16,] 32.483518 41.170256 41.874111
#[17,] 16.471009 23.334594 20.552049
#[18,] 15.561002 27.706218 13.277251
#[19,] 13.375505 19.056233 24.006360
#[20,] 30.191274 42.999607 12.498551
这篇关于两组点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!