Skip to Content

R – Vector Cumulative Product (With Examples)

Vectors are used to store one-dimensional data of the same type in R. In this tutorial, we will look at how to get the cumulative product of a vector in R with the help of some examples.

What is the cumulative product?

The cumulative product of a series of values is the product of values up to that value in our series. For example, for a vector of three values (a1, a2, and a3), the cumulative product would be a1, a1*a2, and a1*a2*a3. The following image illustrates this with an example.

cumulative product of a vector of four numbers

In the above image, we have four values 1, 2, 3, and 4. The cumulative product for these values is 1, 1*2, 1*2*3, and 1*2*3*4 respectively. Note that the order in which these values appear is important when computing the cumulative product.

How to calculate the cumulative product of a vector in R?

You can use the cumprod() function in R to compute the cumulative product of the values in a vector. Pass the vector as an argument to the function. The following is the syntax –

# cumulative product of vector x
cumprod(x)

It returns a vector containing the cumulative product of the values in the passed vector.

Examples

Let’s now look at some examples of using the above syntax.

Cumulative product of a vector of numbers

Let’s create a vector of some numbers and use the cumprod() function to calculate its cumulative prod. For example, let’s compute the cumulative product for the vector c(1, 2, 3, 4).

# create a vector
vec <- c(1, 2, 3, 4)
# cumulative product of vector
print(cumprod(vec))

Output:

[1]  1  2  6 24

We print the resulting cumulative product. You can see that each value in the cumulative product vector is the product of all values till that particular index from the original vector.

Cumulative product of a vector with NA values

What would happen if you apply the cumprod() function to a vector containing some NA values?

Let’s find out.

For this, we will create a vector with some NA values and then apply the cumprod() function.

# create a vector with NA values
vec <- c(1, 2, NA, 3, NA, 4)
# cumulative product of vector
print(cumprod(vec))

Output:

[1]  1  2 NA NA NA NA

You can see that we get the cumulative product till we encounter the first NA in our vector. From this point onwards, the resulting cumulative product for all the values is NA. This happens because performing any arithmetic operation with NA results in an NA in R.

If you want to compute the cumulative product irrespective of the NA values, you can first remove the NA values from the vector and then apply the cumprod() function.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.