I have a matrix (22467 rows and 4 columns) of x and y GPS locations (decimal degrees) for multiple time steps (one hour) for multiple individuals (ID, n=13). Example of dataset (saved as csv file):
ID Time x y
98427 01:00 43.97426 -59.56677
98427 02:00 43.97424 -60.56970
98428 01:00 43.97434 -60.52222
98428 02:00 43.97435 -59.24356
98429 01:00 43.97657 -59.36576
98429 02:00 43.97432 -59.98674
I would like to calculate the distance between each individual, for all combinations, at each time step. Thus, at Time = 01:00, distance between 98427 and 98428, 98427 and 98429, 98428 and 98429, etc. How can I do this in R?
library(plyr)
data = iris
data = data[c(1:5, 81:85, 141:145), 3:5]
data$time = rep(1:5, 3)
dlply(data, .(time), function(x) {dist(x[ , 1:2])})
I just played with iris dataset, but methodology is very simillar.
1. Split the data by time
2. Use column x and y and pass to dist() function which returns matrix of distances
3. Store each as list
Then you can pull values from list, which has each entry named as time.
Update : Sorry about naively thinking as euclidean distance. Here's somewhat crude implementation of Haversine distance.
library(geosphere)
havdist = function(x) {
n = dim(x)[1]
res = matrix(NA, nrow = n, ncol = n)
for (i in 1:n) {
k = 1
for (j in k:n) {
res[i, j] = res[j, i] = distHaversine(a[i, ], a[j, ])
}
n = n - 1
k = k + 1
}
return(res)
}
Then supply havdist instead of dist in above dlply method.
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.