我正在使用在xy坐标平面上跟踪对象的跟踪软件。我一直在使用它来计算y坐标中的位置变化,但是跟踪软件中遗留了一些伪像。通常,跟踪器会跳到一个角,在那里停留几帧,然后跳回去。我的数据最终看起来像这样:
yposition <- c(400,402,403,404,405,407,80,81,83,80,402,401,399,397, 398, 398, 653, 653, 654, 395, 392, 391)
dataframe <- data.frame(yposition)
velocity <- c(0,2,1,1,1,2,327,1,2,2,-322, -1,-1, -2, 1, 0, -255, 0, 1, 259, -3, -1)
dataframe <- cbind(dataframe,velocity)
在这种情况下,伪影将是当yposition跳到80并返回时,以及当yposition跳到653并返回时。有没有办法删除与这些工件相对应的changeinposition值(在这种情况下,从327到-322的元素,从-255到259的元素)?
这在实践中有点棘手,因为目前尚不清楚如何识别您的基本水平而不是跳跃。
如果我们假设您的系列总是从normal
级别开始,则可以结合使用diff
和cumsum
从信号中排除跳跃:
jump.level <- 150 # this value is arbitrary
yposition[abs(cumsum(c(0, diff(yposition)))) < jump.level]
# [1] 400 402 403 404 405 407 402 401 399 397 398 398 395 392 391
要过滤data.frame,您可以使用which
提取与上面的条件匹配的元素的索引:
i <- which(abs(cumsum(c(0, diff(yposition)))) < jump.level)
dataframe[i, ]
如果去除是基于速度:
# threshold for velocity, remove rows and following rows where speed is changed abruptly until meeting another abrupt change.
threshold <- 5
isAbnormal <- abs(dataframe$velocity) > threshold
indexes <- which(isAbnormal)
由于我们需要删除的开始行和结尾行始终成对出现,所以我们知道我们需要从7到11以及从17到20删除行。
> indexes
[1] 7 11 17 20
我们需要删除两对(7:11
和17:20
):
pairs <- split(indexes,rep(1:(length(indexes)/2),times = 1,each = 2))
> pairs
$`1`
[1] 7 11
$`2`
[1] 17 20
将对转换为向量,并删除这些行:
remove_rows <- Reduce(
function(x,y) c(x,y),
Map(function(x) x[1]:x[2],pairs)
)
> remove_rows
[1] 7 8 9 10 11 17 18 19 20
> dataframe[-remove_rows,]
yposition velocity
1 400 0
2 402 2
3 403 1
4 404 1
5 405 1
6 407 2
12 401 -1
13 399 -1
14 397 -2
15 398 1
16 398 0
21 392 -3
22 391 -1