如何使用 R 计算两组点(坐标)之间的欧氏距离的房子距离



我正在尝试计算房屋ax,bx之间的欧几里得距离,...从表中。这是我的数据看起来像:

df <- data.frame(house=c(letters[1:10],"x"),long=c(11,15,19,18,16,23,25,21,23,29,19), 
lat=c(26,29,28,30,26,25,22,24,25,24,25), 
location=(c(rep("city", 5),rep("district", 5), "null")))

我尝试用欧几里得公式计算:

euclid<- function(x1,x2, y1,y2) {
euclid= sqrt((x1-x2)^2+(y1-y2)^2)
return(euclid)
}

我正在寻找此输出:

House   long    lat **Distance to X**
h       21      24  2.24
c       19      28  3
e       16      26  3.16
f       23      25  4
i       23      25  4
d       18      30  5.1
b       15      29  5.66
g       25      22  6.71
a       11      26  8.06
j       29      24  10.05

如何将公式循环到长值和纬度值?

还有dist()函数。请注意,rownames步骤是为了使输出更具可读性:

rownames(df) <- df[['house']]
dist(df[, c('long', 'lat')])
# added round(..., 1) to make this output
a    b    c    d    e    f    g    h    i    j
b  5.0                                             
c  8.2  4.1                                        
d  8.1  3.2  2.2                                   
e  5.0  3.2  3.6  4.5                              
f 12.0  8.9  5.0  7.1  7.1                         
g 14.6 12.2  8.5 10.6  9.8  3.6                    
h 10.2  7.8  4.5  6.7  5.4  2.2  4.5               
i 12.0  8.9  5.0  7.1  7.1  0.0  3.6  2.2          
j 18.1 14.9 10.8 12.5 13.2  6.1  4.5  8.0  6.1     
x  8.1  5.7  3.0  5.1  3.2  4.0  6.7  2.2  4.0 10.0

若要获取预期的输出,可以将dist类转换为矩阵和子集:

as.matrix(dist(df[, c('long', 'lat')]))[11, -11]
a    b    c    d    e    f    g    h    i    j 
8.1  5.7  3.0  5.1  3.2  4.0  6.7  2.2  4.0 10.0 
df$distance_to_x <- as.matrix(dist(df[, c('long', 'lat')]))[11, ]
df
house long lat location distance_to_x
a     a   11  26     city      8.062258
b     b   15  29     city      5.656854
c     c   19  28     city      3.000000
d     d   18  30     city      5.099020
e     e   16  26     city      3.162278
f     f   23  25 district      4.000000
g     g   25  22 district      6.708204
h     h   21  24 district      2.236068
i     i   23  25 district      4.000000
j     j   29  24 district     10.049876
x     x   19  25     null      0.000000

如果您想按照@nicola建议使用您的函数。使用with()也会有所帮助:

with(df, euclid(long, long[house =='x'], lat, lat[house == 'x']))

除了按@Coledist()的方法外,您还可以使用outer()来制作它,即

# form complex-valued coordinates
z <- with(df,long + 1i*lat)
# calculate distance between complex numbers
df$distance2x <- as.numeric(abs(outer(z,z,"-"))[which(df$house == "x"),])

这样

> df
house long lat location distance2x
1      a   11  26     city   8.062258
2      b   15  29     city   5.656854
3      c   19  28     city   3.000000
4      d   18  30     city   5.099020
5      e   16  26     city   3.162278
6      f   23  25 district   4.000000
7      g   25  22 district   6.708204
8      h   21  24 district   2.236068
9      i   23  25 district   4.000000
10     j   29  24 district  10.049876
11     x   19  25     null   0.000000

注意:这个想法是形成复值坐标,并在两个房子之间的差异上使用abs()

最新更新