关于r:我正在尝试计算每个未感染点与一个感染点之间的距离之和

 2021-04-27 

I am trying to calculate the sum of distances between every uninfected point and an infected point

我使用此数据对感染状态的个体进行了随机绘图。frame:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
structure(list(xcoord = c(22L, 38L, 46L, 86L, 66L, 97L, 51L,
87L, 24L, 45L, 61L, 96L, 91L, 90L, 49L, 17L, 25L, 7L, 6L, 74L,
76L, 94L, 27L, 99L, 39L, 83L, 40L, 41L, 9L, 84L, 88L, 63L, 43L,
12L, 93L, 1L, 54L, 13L, 33L, 68L, 20L, 64L, 85L, 3L, 28L, 19L,
23L, 81L, 79L, 5L, 78L, 56L, 8L, 26L, 73L, 37L, 71L, 14L, 11L,
55L), ycoord = c(8L, 35L, 77L, 5L, 68L, 22L, 62L, 86L, 85L, 91L,
74L, 71L, 11L, 1L, 34L, 76L, 13L, 57L, 10L, 29L, 14L, 47L, 80L,
45L, 83L, 56L, 17L, 70L, 55L, 51L, 6L, 46L, 16L, 58L, 18L, 72L,
54L, 38L, 41L, 73L, 69L, 33L, 100L, 50L, 37L, 65L, 25L, 90L,
20L, 95L, 88L, 19L, 67L, 42L, 78L, 53L, 15L, 96L, 66L, 84L),
infectionstatus = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -60L), class ="data.frame")

我想做的是计算每个未感染个体与被感染个体之间的距离。这样一来,一旦我在每个未感染点和被感染点之间保持距离,就可以使用此信息根据该信息来发展成为被感染功能的可能性。如何计算每个未感染点与一个感染点之间的距离?


如果数据集较小,则可以使用outer计算距离。

1
2
3
4
5
6
7
8
9
10
11
12
13
i <- x$infectionstatus == 1
tt <- outer(x$xcoord[i], x$xcoord[!i],"-")^2 + outer(x$ycoord[i], x$ycoord[!i],"-")^2
sqrt(apply(tt, 2, min))
# [1]  81.884064  57.271284  56.603887  42.755117  35.000000  25.179357
# [7]  45.541190  39.623226  79.649231  65.855903  42.638011  24.083189
#[13]  36.124784  46.173586  46.840154  82.280010  76.922038  87.572827
#[19]  95.462034  26.907248  37.589892  74.686009   5.385165  65.734314
#[25]  14.212670  61.773781  57.775427  85.375641  10.770330  41.436699
#[31]  31.016125  59.682493  82.734515  29.017236  96.301610  40.607881
#[37]  81.498466  61.294372  36.769553  77.201036  33.105891  53.758720
#[43]  91.049437  66.753277  77.129761  74.330344  44.922155  30.886890
#[49] 101.118742  44.011362  47.201695  88.294960  68.183576  37.443290
#[55]  57.314920  39.408121  93.813645  85.146932  53.758720

1
2
3
4
5
6
7
8
9
# Calculate the pairwase distance matrix for every individual to the rest.
if (!require("rdist")) { install.packages("rdist"); require("rdist") }
distances <- pdist(data[,1:2], metric ="euclidean")

# dataframe position of the infected individual.
position_infected_individual <- which(data[,3]==1)

# distances from this infected individual to the rest (we eliminate the distance from itself that is 0)
dist_from_infected <- distances[-(position_infected_individual),position_infected_individual]