How to compute distance that x and y coordinates are in two different dataframes?

Hi,
I want to compute the distance. While x and y coordinates are in two different dataframes. Now we let all but the first column be subtracted from the first column. So we may compute (x1-x)^2, (x2-x)^2, (x3-x)^2, (y1-y)^2, (y2-y)^2, (y3-y)^2. And then we may compute the distance, sqrt( (x1-x)^2+(y1-y)^2), sqrt( (x2-x)^2+(y2-y)^2), sqrt( (x3-x)^2+(y3-y)^2). And then all the distance may form a new dataframe, col and row numbers are equal to d1, 6 rows and 4 cols.
Well, i could only calculate square value about x and y separately. What's more, if there is NA in d1 and d2, method would be different? Thank you in advance.

d1<-data.frame(x=c(10.3,13.4,14.3,16.4,17.4,19.1),
               x1=c(9.6,13,13.4,17.4,18,19.2),
               x2=c(10.1,12.7,15.5,15.5,18.4,20.1),
               x3=c(9.2,12.7,13,15.8,16.4,20.2))
d2<-data.frame(y=c(10.1,13.3,13.3,13.7,14.2,11.4),
               y1=c(10.5,13.2,13.3,14.2,14.9,16.5),
               y2=c(9.1,13.2,12.8,12.8,14,11.6),
               y3=c(10.8,12.9,13.2,11.9,13.7,11))
d3<-(d1-d1[,1])^2
d4<-(d2-d2[,1])^2

Created on 2022-11-05 with reprex v2.0.2

I'm not sure I completely understood. Can this be helpful?

library(tidyverse)

tot <- full_join(d1, d2)

# everything - x
tot-tot[,1]

# everything - y
tot-tot[,6]

With what you already have, you can simply do:

sqrt(d3+d4)
  x        x1        x2        x3
1 0 0.8062258 1.0198039 1.3038405
2 0 0.4123106 0.7071068 0.8062258
3 0 0.9000000 1.3000000 1.3038405
4 0 1.1180340 1.2727922 1.8973666
5 0 0.9219544 1.0198039 1.1180340
6 0 5.1009803 1.0198039 1.1704700

this adds each element of d3 to each element of d4, then you take the sqrt() for each element.

Or if you separate the reference x and y:

x0=c(10.3,13.4,14.3,16.4,17.4,19.1)
y0=c(10.1,13.3,13.3,13.7,14.2,11.4)


x<-data.frame(x1=c(9.6,13,13.4,17.4,18,19.2),
              x2=c(10.1,12.7,15.5,15.5,18.4,20.1),
              x3=c(9.2,12.7,13,15.8,16.4,20.2))
y<-data.frame(y1=c(10.5,13.2,13.3,14.2,14.9,16.5),
              y2=c(9.1,13.2,12.8,12.8,14,11.6),
              y3=c(10.8,12.9,13.2,11.9,13.7,11))

sqrt((x-x0)^2 + (y-y0)^2)
#>          x1        x2        x3
#> 1 0.8062258 1.0198039 1.3038405
#> 2 0.4123106 0.7071068 0.8062258
#> 3 0.9000000 1.3000000 1.3038405
#> 4 1.1180340 1.2727922 1.8973666
#> 5 0.9219544 1.0198039 1.1180340
#> 6 5.1009803 1.0198039 1.1704700

Created on 2022-11-05 by the reprex package (v2.0.1)

Or in such case, it's also common to use matrices (not necessarily better, depends what you do with it afterwards):

x0=c(10.3,13.4,14.3,16.4,17.4,19.1)
y0=c(10.1,13.3,13.3,13.7,14.2,11.4)

all_x <- list(x1=c(9.6,13,13.4,17.4,18,19.2),
              x2=c(10.1,12.7,15.5,15.5,18.4,20.1),
              x3=c(9.2,12.7,13,15.8,16.4,20.2))

all_y <- list(y1=c(10.5,13.2,13.3,14.2,14.9,16.5),
              y2=c(9.1,13.2,12.8,12.8,14,11.6),
              y3=c(10.8,12.9,13.2,11.9,13.7,11))

X <- matrix(unlist(all_x), ncol = 3)
Y <- matrix(unlist(all_y), ncol = 3)

sqrt((X-x0)^2 + (Y-y0)^2)

If there are NAs for one point, that just means the distance for that point will be NA. Otherwise how can you compute the distance to a point if you don't know where that point is?

Also it's not completely clear to me what the rows represent, and the 3 columns (x1, y1), (x2, y2) and (x3, y3). Do you have 3*6 points total? In that case wouldn't it make more sense to represent them in a single data frame with 3*6=18 rows?

1 Like

It does work. I am very grateful to your effort on this matter. My problem has been solved.
Well, x1 and y1 , x2 and y2, x3 and y3 are in a one-to-one, ordered arrangement. For example, 9.6(x1) and 10.5(y1), 10.1(x2) and9.1(y2), 9.2(x3)and10.8(y3). Therefore, except the reference point, there are 18points.
And the result you show me is what i thought, i accept it.

Thank you for your help. It seems to be a different idea.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.