Reputation: 354
I have a random plot of individuals with infection status using this data.frame:
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")
What I would like to do is calculate the distance between every uninfected individual with the infected individual. This is so that once I have the distance between every uninfected and the infected point, I can use this information to develop a likelihood of becoming infected function which is based on this information. How do I calculate the distance between each uninfected point and the one infected point?
Upvotes: 1
Views: 57
Reputation: 39717
You can use outer
to calcualte the distances if the dataset is small.
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
Upvotes: 3
Reputation: 354
# 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]
Upvotes: 1