简体   繁体   English

如何在R中同时计算多个人之间的地理距离

[英]How to calculate geographical distance between multiple individuals at same timestep in R

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). 我有一个x和y GPS位置(十进制度)的矩阵(22467行和4列),用于多个人(ID,n = 13)的多个时间步(一小时)。 Example of dataset (saved as csv file): 数据集示例(保存为csv文件):

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? 因此,在时间= 01:00处,98427和98428、98427和98429、98428和98429之间的距离等。如何在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 1.按时间拆分数据
2. Use column x and y and pass to dist() function which returns matrix of distances 2.使用x和y列并传递给dist()函数,该函数返回距离矩阵
3. Store each as list 3.将每个存储为列表

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. 这是Haversine距离的粗略实现。

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. 然后在上述dlply方法中提供havdist而不是dist。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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