在 R 编程中计算数值对象的累积和 - cumsum()函数
累积和可以定义为一组数字的总和,因为总和值随着数字序列的增长而增长。
R 语言中的cumsum()函数用于计算作为参数传递的向量的累积和。
Syntax: cumsum(x)
Parameters:
x: Numeric Object
示例 1:
Python3
# R program to illustrate
# the use of cumsum() Function
# Calling cumsum() Function
cumsum(1:4)
cumsum(-1:-6)
Python3
# R program to illustrate
# the use of cumsum() Function
# Creating Vectors
x1 <- c(2, 4, 5, 7)
x2 <- c(2.4, 5.6, 3.4)
# Calling cumsum() Function
cumsum(x1)
cumsum(x2)
输出:
[1] 1 3 6 10
[1] -1 -3 -6 -10 -15 -21
示例 2:
Python3
# R program to illustrate
# the use of cumsum() Function
# Creating Vectors
x1 <- c(2, 4, 5, 7)
x2 <- c(2.4, 5.6, 3.4)
# Calling cumsum() Function
cumsum(x1)
cumsum(x2)
输出:
[1] 2 6 11 18
[1] 2.4 8.0 11.4