展开坐标列表以获取 R 中一个组内的所有组合

Expand list of coordinates to get all combinations within a group in R

我有一个 DF,大约有 100 万条记录。每条记录都包括纬度和经度,记录按照示例数据进行分组(除了更大的组)

data.frame(Latitude=c(-30.25,-30.89,-30.48,-30.10), 
           Longitude=c(116.321,116.98,116.78,116.38), 
           grp=c('a','a','b','b'))

在每组中,我需要找到任意两组坐标之间的最大距离。一旦我在 DF 中拥有所有坐标组合,我就可以毫无问题地计算距离,但无法有效地将每个组合放入看起来像这样的 DF

data.frame(Latitude1=c(-30.25,-30.25,-30.89,-30.89,-30.48,-30.48,-30.10,-30.10), 
           Longitude1=c(116.321,116.32,116.98,116.98,116.78,116.78,116.38,116.38), 
           Latitude2=c(-30.25,-30.89,-30.25,-30.89,-30.48,-30.10,-30.48,-30.10), 
           Longitude2=c(116.321,116.98,116.98,116.321,116.78,116.38,116.38,116.78), 
           grp=c('a','a','a','a','b','b','b','b'))

我已经编写了一个嵌套循环来执行此操作,但它非常慢,我确信有更好的方法。 我查看了复制列并使用 expand.grid,但可以找到如何在多个因素下使用它 任何帮助,将不胜感激。谢谢

像这样的东西让你入门怎么样?我们利用 geosphere::distm 来计算距离(这里是测地线距离):

  1. 根据grpdata.frame分组:

    lst <- split(df, df$grp)
    
  2. 计算测地距离

    library(geosphere);
    dist <- lapply(lst, function(x) distm(x[, c("Longitude", "Latitude")]));
    
  3. 结果是一个list的对称距离矩阵,其中rows/columns对应记录

    dist;
    #$a
    #         [,1]     [,2]
    #[1,]     0.00 95029.27
    #[2,] 95029.27     0.00
    #
    #$b
    #         [,1]     [,2]
    #[1,]     0.00 57056.28
    #[2,] 57056.28     0.00
    

然后您可以根据每组的最小距离过滤记录。每组你只给 2 分,所以提取最大距离是微不足道的,因为只有一个。

如果您习惯使用开发/non-released 包,我已经编写了 spatialdatatable 来对 data.table 对象进行高效的地理*计算。

这是一个处理 100,000 行数据的解决方案。步骤是

  1. 将数据与其自身相结合,为您提供庞大的 point-to-point 数据集
  2. 计算每对点之间的距离(使用haversine距离)
  3. Select每组内的最大距离。

library(data.table)
# devtools::install_github("SymbolixAU/spatialdatatable")
library(spatialdatatable)


## generate random data
lons <- sample(0:180, 1e5, replace = T)
lats <- sample(-90:1, 1e5, replace = T)
grp <- sample(letters, 1e5, replace = T)
df <- data.frame(lon = lons, lat = lats, grp = grp)

## set as a data.table object, and assign an 'id' to each point
setDT(df)
df[, id := .I]

## 1. join the df to itself to give all points to all other points
df <- df[
    df
    , on = "grp"
    , nomatch = 0
    , allow.cartesian = T
    ][id != i.id]   ## remove points joined with themselves

## 2. calculate distances
df[, dist := spatialdatatable::dtHaversine(lat, lon, i.lat, i.lon)]

## 3. select greatest distance per group
df[ df[, .I[which.max(dist)], by = grp]$V1 ][order(grp)]

#     lon lat grp    id i.lon i.lat  i.id     dist
#  1:   1   0   a 27726   180     0 10996 19903920
#  2:   1   1   b 63425   180    -3 57218 19766508
#  3:   1   1   c 18255   177    -2    56 19556799
#  4:   0  -1   d 43560   179     0  8518 19857865
#  5: 178  -2   e 37485     0     0 34482 19700640
#  6:   1  -2   f 79879   180     1 70765 19857889
#  7: 178   1   g 84268     1    -3 44148 19614379
#  8: 178  -5   h 49310     1     1  1306 19459455
#  9:   0   1   i 92786   179    -2 55584 19857889
# 10: 180   0   j 92704     0     0 36757 20015115
# 11:   0  -1   k 75760   180     0 71050 19903920
# 12:   0  -1   l 42202   180     0 10839 19903920
# 13:   0   1   m 73069   177    -2  2708 19663598
# 14:   0   1   n 10830   180    -1  1236 20015115
# 15:   3  -2   o 43380   180     1  3829 19663598
# 16: 179   1   p 95740     0    -1  3061 19903937
# 17:   0  -1   q 49476   180     0 18257 19903920
# 18: 180   0   r 96154     1     0 42435 19903920
# 19: 180  -1   s 82115     1     0 47784 19857865
# 20: 178  -2   t 42861     0     0 22020 19700640
# 21: 180   0   u 22965     0    -1 12158 19903920
# 22: 178   0   v 18557     0    -2 17457 19700640
# 23: 178  -2   w 58321     1    -1 13906 19543390
# 24:   0  -1   x 93181   177    -3 67084 19459211
# 25:   0  -1   y 46491   178     1  5548 19792759
# 26:   3   1   z 43109   180    -3   769 19614379

  • 相比于library(geosphere)