I created 2 data frames with geom columns (of POINT type). Now I would like to calculate distance between each pair of points, e.g. point from 1st row in first df with point from 1st row in second df etc. Here are my data frames:
df1 <- table %>%
st_as_sf(coords = c("lonCust","latCust"), crs = 4326)
df2 <- table %>%
st_as_sf(coords = c("lonApp","latApp"), crs = 4326)
I used st_distance:
distance <- st_distance(df1$geometry,df2$geometry)
but I got a matrix where distance is calculated for each-each pair from both geom columns:
[,1] [,2] [,3] [,4] [,5] ...
[1,] 139.7924 7735.5718 15225.02995 558.104089 1016.58121
[2,] 8503.0544 755.2915 8764.75396 7957.289600 8788.02800
[3,] 15306.5855 9336.9008 18.96914 14876.589918 15929.51643
[4,] 548.3045 7232.0164 14898.70637 8.094068 1078.38236
[5,] 911.5635 8084.3086 15993.36365 1127.730022 46.97799
.
.
I wanted distance to be calculated in one column, only between corresponding geom rows:
[,1]
[1,] 139.7924
[2,] 8503.0544
[3,] 15306.5855
[4,] 548.3045
[5,] 911.5635
.
.
I read about geosphere package but sf has very nice st_distance function to measure distance, I wanted to use it. And most importantly, do I need first to join those data frames? Simple inner_join from dplyr doesn't allow to join two spatial data frames, st_join on the other hand is not an option for me here bacause I don't want to join by geometries (geometries in two data frames are totally different)