首页 文章

对R中DF列子集的所有可能组合执行函数

提问于
浏览
1

我想计算行/纬度坐标对之间的距离 . 这可以通过earth.dist等各种功能轻松完成 . 我被困的地方是,我希望这是每晚数据质量检查过程的一部分,其中对的数量会发生变化 . 每一行都是独特的主题/人 . 有些日子,一些科目可以有四组坐标,有些日子最大可能是三组 . 是否有一种优雅的方式来执行此计算,例如,使用由以下形式形成的所有可能组合:

combn(geototal, 2])

,其中geototal是给定日期的坐标集数,例如x = 4表示集合:

latitude.1,longitude.1,latitude.2,longitude.2,latitude.3,longitude.3 latitude.4,longitude.4 .

我当前的循环看起来像这样,但当然错过了许多可能的组合,尤其是当X大于4时 .

x = 1; y = 2 
while(x <= geototal) 
{
  if (y > geototal) break;
  eval(parse(text = sprintf("df$distance%d_%d = earth.dist(longitude.%d,latitude.%d,longitude.%d,latitude.%d)", x, y, x, x, y, y)));
  x <- x + 1; 
  y <- y + 1;
}

感谢您对此的任何想法!

1 回答

  • 0

    尝试这样的事情

    # Using a built in dataset from library(fossil)
    data(fdata.lats)
    df = fdata.lats@coords
    
    # Function to do calculate pairwise distance
    foo = function(df) {
      # Find the number of pairs
      n = nrow(df)
      # Find all combination
      l = t(combn(n, 2))
      # Loop over all combination and calculate distance, store output in vector
      t = apply(l, 1, function(x) {earth.dist(df[x,])})
      # Return the list of pairs and their distance, modify here if you want to print something instead
      cbind(l, t)
    }
    
    # Test run
    foo(df)
    
                        t
     [1,]  1  2  893.4992
     [2,]  1  3  776.3101
     [3,]  1  4 1101.1145
     [4,]  1  5 1477.4800
     [5,]  1  6  444.9052
     [6,]  1  7  456.5888
     [7,]  1  8 1559.4614
     [8,]  1  9 1435.2985
     [9,]  1 10 1481.0119
    [10,]  1 11 1152.0352
    [11,]  1 12  870.4960
    [12,]  2  3  867.2648
    [13,]  2  4  777.6345
    [14,]  2  5  860.9163
    ...
    

相关问题