必看1:http://www.iikx.com/news/statistics/1824.html
必看2:https://www.plob.org/article/3176.html
https://blog.csdn.net/ARPOSPF/article/details/86649066
两个函数备注
diff():计算向量中两元素的差值,包含三个参数:x,向量或矩阵,如果是矩阵,则按照列计算差值;lag,默认是1,默认计算间隔为1的两个元素的差值;differences,默认为1,默认只计算一次差值,不会再累计计算
https://likan.info/cn/post/diff-function-in-r/
> diff(1:5)
[1] 1 1 1 1
> diff(1:5,lag=2)
[1] 2 2 2
> diff(1:5,differences=2)
[1] 0 0 0
> diff(matrix(1:20,nrow = 5))
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 1 1 1
[3,] 1 1 1 1
[4,] 1 1 1 1
replicate,与rep类似,进行重复取值,区别是:1、replicate中重复次数设置是第一个参数,rep是第二个;2、rep中输入不是向量化,没有办法更新,而replicate可以
https://www.jianshu.com/p/7d7862c72c4a
rep(runif(1),5)
#[1] 0.7548687 0.7548687 0.7548687 0.7548687 0.7548687
replicate(5,runif(1))
[1] 0.5315511 0.9896081 0.1711190 0.4024064 0.7224799