R语言中diff函数

R语言中diff函数,表示滞后差分

1、测试1

test <- sample(1:10,5)
test
a <- diff(test)    ## diff表示后一个数减去前一个数
a

 2、测试2

test <- sample(1:10,5)
test
a <- diff(test)
a
b <- diff(test,differences = 2)   ## differences 参数表示连续执行两次diff
b

 3、测试3

test <- sample(1:10,5)
test
c <- diff(test, lag = 2)   ## lag表示中间有两个间距的数只差,比如第三数和第一个数只差,以此类推
c

4、测试4  矩阵测试

a <- matrix(c(3,4,2,5,8,2,8,1,7,5,2,5),4,3)
a
diff(a[,1])
diff(a)

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14695911.html