cumprod()

cumprod(x)
Returns: numeric · Updated March 13, 2026 · Base Functions
cumulative vectors math cumprod base

The cumprod() function computes the cumulative product of a vector, returning a vector of the same length where each element is the product of all preceding elements. This makes it handy for compound growth, geometric sequences, and other multiplicative calculations.

Syntax

cumprod(x)

Parameters

ParameterTypeDefaultDescription
xnumericrequiredA numeric vector

Examples

Basic usage

x <- c(1, 2, 3, 4, 5)

cumprod(x)
# [1]   1   2   6  24 120

Working with financial data

# Cumulative returns
prices <- c(100, 102, 98, 105, 110)
returns <- prices / 100
cumprod(returns)
# [1] 1.00 1.02 1.00 1.05 1.10

Geometric progression

# Geometric progression via cumprod
cumprod(rep(2, 5))
# [1]  2  4  8 16 32

# Powers of a number
cumprod(rep(3, 4))
# [1]  3  9 27 81

Common Patterns

Compound growth

# Growth rates
growth <- c(1.1, 1.05, 1.02, 1.08)
cumprod(growth)
# [1] 1.1000 1.1550 1.1781 1.2724

Factorial calculation

# Factorial of 5 = 5!
cumprod(1:5)[5]
# [1] 120

See Also