【问题标题】:calculating distance between two row in a data.table计算 data.table 中两行之间的距离
【发布时间】:2015-12-10 21:42:50
【问题描述】:

问题总结:我正在使用R(版本)中的data.table 包(版本1.9.5)清理鱼遥测数据集(即,通过时间的空间坐标)一台 Windows 7 电脑。一些数据点是错误的(例如,遥测设备拾取回波)。我们可以判断这些点是错误的,因为鱼移动的距离比生物学上可能的距离更远,并且作为异常值脱颖而出。实际数据集包含来自 30 条鱼的超过 2,000,000 行数据,因此使用了data.table 包。

我正在删除相距太远的点(即,行进的距离大于最大距离)。但是,我需要在删除一个点后重新计算点之间的距离,因为有时会在集群中错误记录 2-3 个数据点。目前,我有一个 for 循环可以完成工作,但可能远非最佳,而且我知道我可能缺少 data.table 包中的一些强大工具。

作为技术说明,我的空间尺度足够小,可以使用欧几里得距离,并且我的最大距离标准在生物学上是合理的。

我在哪里寻求帮助:我浏览了 SO 并找到了几个有用的答案,但没有一个完全符合我的问题。具体来说,所有其他答案仅将一列数据与行间进行比较。

  1. answer 使用data.table 比较两行,但只查看一个变量。

  2. 这个answer 看起来很有前途并使用Reduce,但我不知道如何在两列中使用Reduce

  3. 这个answer 使用data.table 的索引功能,但我不知道如何将它与距离函数一起使用。

  4. 最后,这个answer 演示了data.tableroll 函数。但是,我也不知道如何在这个函数中使用两个变量。

这是我的 MVCE:

library(data.table)
## Create dummy data.table
dt <- data.table(fish = 1,
                 time = 1:6,
                 easting = c(1, 2, 10, 11, 3, 4),
                 northing = c(1, 2, 10, 11, 3, 4))
dt[ , dist := 0]

maxDist = 5

## First pass of calculating distances 
for(index in 2:dim(dt)[1]){
    dt[ index,
       dist := as.numeric(dist(dt[c(index -1, index),
                list(easting, northing)]))]
}

## Loop through and remove points until all of the outliers have been
## removed for the data.table. 
while(all(dt[ , dist < maxDist]) == FALSE){
    dt <- copy(dt[ - dt[ , min(which(dist > maxDist))], ])
    ## Loops through and recalculates distance after removing outlier  
    for(index in 2:dim(dt)[1]){
        dt[ index,
           dist := as.numeric(dist(dt[c(index -1, index),
                    list(easting, northing)]))]
    }
}

【问题讨论】:

    标签: r data.table


    【解决方案1】:

    我有点困惑,为什么你不断地重新计算距离(并且不必要地复制数据)而不是只做一次:

    last = 1
    idx = rep(0, nrow(dt))
    for (curr in 1:nrow(dt)) {
      if (dist(dt[c(curr, last), .(easting, northing)]) <= maxDist) {
        idx[curr] = curr
        last = curr
      }
    }
    
    dt[idx]
    #   fish time easting northing
    #1:    1    1       1        1
    #2:    1    2       2        2
    #3:    1    5       3        3
    #4:    1    6       4        4
    

    【讨论】:

    • 我通过了两次数据并进行了复制,因为我无法找出单次通过的解决方案:)。谢谢你的回答!
    猜你喜欢
    • 2018-10-05
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2017-08-27
    • 2015-08-16
    • 1970-01-01
    相关资源
    最近更新 更多