在 R 编程中计算数值对象的累积乘积 – cumprod()函数
R 语言中的cumprod()
函数用于计算作为参数传递的向量的累积乘积。
Syntax: cumprod(x)
Parameters:
x: Numeric Object
示例 1:
# R program to illustrate
# the use of cumprod() Function
# Calling cumprod() Function
cumprod(1:4)
cumprod(-1:-6)
输出:
[1] 1 2 6 24
[1] -1 2 -6 24 -120 720
示例 2:
# R program to illustrate
# the use of cumprod() Function
# Creating Vectors
x1 <- c(2, 4, 5, 7)
x2 <- c(2.4, 5.6, 3.4)
# Calling cumprod() Function
cumprod(x1)
cumprod(x2)
输出:
[1] 2 8 40 280
[1] 2.400 13.440 45.696