【发布时间】: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 并找到了几个有用的答案,但没有一个完全符合我的问题。具体来说,所有其他答案仅将一列数据与行间进行比较。
此answer 使用
data.table比较两行,但只查看一个变量。这个answer 看起来很有前途并使用
Reduce,但我不知道如何在两列中使用Reduce。这个answer 使用
data.table的索引功能,但我不知道如何将它与距离函数一起使用。最后,这个answer 演示了
data.table的roll函数。但是,我也不知道如何在这个函数中使用两个变量。
这是我的 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