Returns a vector whose elements are the cumulative sums, products, minima or maxima of the elements of the argument.
Usage
cumsum(x)
cumprod(x)
cummax(x)
cummin(x)
- cumsum 可以实现整体的累加
> cumsum(1:10)
[1] 1 3 6 10 15 21 28 36 45 55
向量中第一个元素为起始,后面是其需要加的数。
- cumprod(x) 累乘
> cumprod(1:5)
[1] 1 2 6 24 120
- cummax 保留出现的最大的那个数,并不断输出
> cummax(c(3:1, 2:0, 4:2))
[1] 3 3 3 3 3 3 4 4 4
- cummin 同理最小的那个
> cummin(c(3:1, 2:0, 4:2))
[1] 3 2 1 1 1 0 0 0 0