R函数从向量中的后续值中减去向量中连续值之间的差值

时间:2017-06-06 13:34:20

标签: r function vector time-series

我有一个包含重量数据时间序列的数字向量。我需要做的是通过识别连续权重之间的不切实际的差异并从系列中的所有后续值中减去该变化来消除权重变化假象。

例如,在系列:c(5,4,6,8,8,9,8,12,10,100,101,101)中,我会将第9项和第10项(100 - 10 = 90)之间的增量权重确定为一个神器,我会通过从后续值减去90来纠正它,产生c(5,4,6,8,8,9,8,12,10,10,11,11)。

原则上,我的代码看起来像:

cancel_artifacts <- function(weights, delta_max) {
    for (i in 0:length(weights)) {
        if (weights[i] - weights[i-1] > abs(delta_max)) {
            weights[i:length(weights)] <- weights[i:length(weights)] - (weights[i] - weights[i-1])
        }
    }

显然我的语法是一场灾难。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您可以采用矢量化方式执行此操作:

remove_artifacts <- function(weights, delta_max) {
  # calculate deltas, and set first delta to zero
  dw <- c(0, diff(x))
  # create vector of zeros and abs(observations) > delta_max
  # dw * (logical vector) results in either:
  # dw * 0 (if FALSE)
  # dw * 1 (if TRUE)
  dm <- dw * (abs(dw) > delta_max)
  # subtract the cumulative sum of observations > delta_max
  return(weights - cumsum(dm))
}
x <- c(5, 4, 6, 8, 8, 9, 8, 12, 10, 100, 101, 101)
remove_artifacts(x, 50)
# [1]  5  4  6  8  8  9  8 12 10 10 11 11