简体   繁体   中英

Calculate distance from one point to the others by R

I would like to calculate the distance from just one point to the others. From the matrix below, you can see that the distance from all points is being calculated concurrently. But I would like only one point to the rest. How do I define this in the formula? I would like, for example, to know the distance from property 6 to 19 other properties, so we would have only one line.

library(geosphere)
df<-structure(list(Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,  -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, 
+ -23.9, -23.9, -23.9, -23.9, -23.9), Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.7, 
+ -49.7, -49.7, -49.7, -49.7, -49.6, -49.6, -49.6, -49.6), Production = c(526, 350, 526, 469, 285, 175, 175, 350, 350, 175, 350, 175, 175, 364, 
+ 175, 175, 350, 45.5, 54.6)), class = "data.frame", row.names = c(NA, -19L))
coordinaties<-df[,1:2]
d<-as.dist(distm(coordinaties[,2:1]))
data.matrix(d)

    1        2        3        4        5        6        7        8
1      0.00     0.00 11075.61 11075.61 11075.61 11075.61 11075.61 11075.61
2      0.00     0.00 11075.61 11075.61 11075.61 11075.61 11075.61 11075.61
3  11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
4  11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
5  11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
6  11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
7  11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
8  11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
9  11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
10 11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
11 15048.01 15048.01 10183.02 10183.02 10183.02 10183.02 10183.02 10183.02
12 15048.01 15048.01 10183.02 10183.02 10183.02 10183.02 10183.02 10183.02
13 15048.01 15048.01 10183.02 10183.02 10183.02 10183.02 10183.02 10183.02
14 15048.01 15048.01 10183.02 10183.02 10183.02 10183.02 10183.02 10183.02
15 15048.01 15048.01 10183.02 10183.02 10183.02 10183.02 10183.02 10183.02
16 11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
17 11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
18 11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
19 11075.61 11075.61     0.00     0.00     0.00     0.00     0.00     0.00
          9       10       11       12       13       14       15       16
1  11075.61 11075.61 15048.01 15048.01 15048.01 15048.01 15048.01 11075.61
2  11075.61 11075.61 15048.01 15048.01 15048.01 15048.01 15048.01 11075.61
3      0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
4      0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
5      0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
6      0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
7      0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
8      0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
9      0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
10     0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
11 10183.02 10183.02     0.00     0.00     0.00     0.00     0.00 10183.02
12 10183.02 10183.02     0.00     0.00     0.00     0.00     0.00 10183.02
13 10183.02 10183.02     0.00     0.00     0.00     0.00     0.00 10183.02
14 10183.02 10183.02     0.00     0.00     0.00     0.00     0.00 10183.02
15 10183.02 10183.02     0.00     0.00     0.00     0.00     0.00 10183.02
16     0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
17     0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
18     0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
19     0.00     0.00 10183.02 10183.02 10183.02 10183.02 10183.02     0.00
         17       18       19
1  11075.61 11075.61 11075.61
2  11075.61 11075.61 11075.61
3      0.00     0.00     0.00
4      0.00     0.00     0.00
5      0.00     0.00     0.00
6      0.00     0.00     0.00
7      0.00     0.00     0.00
8      0.00     0.00     0.00
9      0.00     0.00     0.00
10     0.00     0.00     0.00
11 10183.02 10183.02 10183.02
12 10183.02 10183.02 10183.02
13 10183.02 10183.02 10183.02
14 10183.02 10183.02 10183.02
15 10183.02 10183.02 10183.02
16     0.00     0.00     0.00
17     0.00     0.00     0.00
18     0.00     0.00     0.00
19     0.00     0.00     0.00

Thanks!!!

See ?distm : you can use two sets of points:

distm(coordinaties[6,2:1],coordinaties[-6,2:1])

         [,1]     [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]    [,10]    [,11]    [,12]
[1,] 11075.61 11075.61    0    0    0    0    0    0    0 10183.02 10183.02 10183.02
        [,13]    [,14] [,15] [,16] [,17] [,18]
[1,] 10183.02 10183.02     0     0     0     0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM