Open In App

Calculate Cumulative Product of a Numeric Object in R Programming – cumprod() Function

cumprod() function in R Language is used to calculate the cumulative product of the vector passed as argument.

Syntax: cumprod(x)



Parameters:
x: Numeric Object

Example 1:






# R program to illustrate 
# the use of cumprod() Function
  
# Calling cumprod() Function
cumprod(1:4)
cumprod(-1:-6)

Output:

[1]  1  2  6 24
[1]   -1    2   -6   24 -120  720

Example 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)

Output:

[1]   2   8  40 280
[1]  2.400 13.440 45.696
Article Tags :