A iterative and lagging function similar to diff in R, but not just difference?

前端 未结 1 1164
执念已碎
执念已碎 2021-02-10 05:44

The diff function in R returns suitably lagged and iterated differences.

x = c(1, 2, 1, 3, 11, 7, 5)
diff(x)
# [1]  1 -1  2  8 -4 -2
diff(x, lag=2)
[1]  0  1 10          


        
相关标签:
1条回答
  • 2021-02-10 06:33

    You can use zoo::rollapply

    require(zoo)
    x <- c(1, 2, 1, 3, 11, 7, 5)
    rollapply(x, width = 2, FUN = sum)
    ## [1]  3  3  4 14 18 12
    
    0 讨论(0)
提交回复
热议问题