R语言 计算一个数字对象的累积乘积–cumprod()函数
R语言中的 cumprod() 函数用于计算作为参数传递的向量的累积乘积。
语法: cumprod(x)
参数:
x: 数字对象
例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