Maybe this strategy may help
X1 = c(1,2,4,5,1,3,1)
Y1 = c(3,5,6,3,1,5,1)
df1= data.frame(X1,Y1)
X2 = c(2,3,4,3,2,3,2)
Y2 = c(3,4,2,6,4,3,4)
df2= data.frame(X2,Y2)
library(tidyverse)
df1 = df1 %>% mutate(df_type = "data1") %>% select(X = X1, Y = Y1)
df2 = df2 %>% mutate(df_type = "data2") %>% select(X = X2, Y = Y2)
# link data frames by row
df = bind_rows(df1, df2)
dist(cbind(df$X,df$Y))
1 2 3 4 5 6 7 8 9 10 11 12 13
2 2.236068
3 4.242641 2.236068
4 4.000000 3.605551 3.162278
5 2.000000 4.123106 5.830952 4.472136
6 2.828427 1.000000 1.414214 2.828427 4.472136
7 2.000000 4.123106 5.830952 4.472136 0.000000 4.472136
8 1.000000 2.000000 3.605551 3.000000 2.236068 2.236068 2.236068
9 2.236068 1.414214 2.236068 2.236068 3.605551 1.000000 3.605551 1.414214
10 3.162278 3.605551 4.000000 1.414214 3.162278 3.162278 3.162278 2.236068 2.236068
11 3.605551 1.414214 1.000000 3.605551 5.385165 1.000000 5.385165 3.162278 2.000000 4.123106
12 1.414214 1.000000 2.828427 3.162278 3.162278 1.414214 3.162278 1.000000 1.000000 2.828427 2.236068
13 2.000000 2.236068 3.162278 2.000000 2.828427 2.000000 2.828427 1.000000 1.000000 1.414214 3.000000 1.414214
14 1.414214 1.000000 2.828427 3.162278 3.162278 1.414214 3.162278 1.000000 1.000000 2.828427 2.236068 0.000000 1.414214
Then you can create a data.frame with the distances between X and Y. First we need to transform the dist
object into a data frame
df_dist = data.frame(as.matrix(dist(cbind(df$X,df$Y))))
Doing a bit of manipulation it is possible to have the distance between X and Y
df_dist_x = df_dist %>% select(X1:X7) %>%
mutate(row.1 = 1:nrow(df_dist)) %>%
filter(row.1 >= 8) %>%
mutate(Y = paste0("Y",row_number())) %>%
gather(X, distance, X1:X7) %>%
select(X, Y, distance)
head(df_dist_x)
X Y distance
1 X1 Y1 1.000000
2 X1 Y2 2.236068
3 X1 Y3 3.162278
4 X1 Y4 3.605551
5 X1 Y5 1.414214
6 X1 Y6 2.000000