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.
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))
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With