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

时间:2015-04-14 19:35:16

标签: r combn

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

combn(geototal, 2])

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

纬度1,经度1,纬度2,经度2,纬度3,经度3纬度4,经度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 :(得分: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
...
相关问题