How to calculate products with prod() in R

how-to
tidyverse
Learn calculate products with prod() in r with clear examples and explanations.
Published

March 26, 2026

Introduction

The prod() function in R calculates the product of all numeric values in a vector or column. This function is particularly useful when you need to multiply all values together, such as calculating compound growth rates, probability products, or geometric transformations. It works seamlessly with dplyr’s summarize() function for data analysis workflows.

Basic Product Calculation

Let’s start by loading the tidyverse and creating a simple dataset to demonstrate the prod() function.

library(tidyverse)

First, we’ll create a tibble with consecutive integers and calculate their product:

df <- tibble(id = seq(6))
df

This creates a dataset with values 1 through 6. Now let’s calculate the product of all these values:

df |>
  summarize(product = prod(id))

The result is 720, which equals 1 × 2 × 3 × 4 × 5 × 6 (also known as 6 factorial).

Handling Missing Values

The prod() function behaves differently when encountering missing values. Let’s create a dataset with some NA values to see this behavior:

set.seed(1234)
df <- tibble(id = sample(c(NA, seq(5)), 5, replace = FALSE)) 
df

Now let’s see what happens when we calculate the product with missing values:

df |>
  summarize(product = prod(id))

By default, prod() returns NA when any missing values are present in the data. This is a safety feature to alert you that some data is missing.

Removing Missing Values

To calculate the product while ignoring missing values, use the na.rm = TRUE parameter:

df |>
  summarize(product = prod(id, na.rm = TRUE))

This calculates the product of only the non-missing values, giving us a meaningful result even when some data points are unavailable.

Summary

The prod() function is a straightforward tool for multiplying all values in a dataset. Remember that it returns NA by default when missing values are present, so use na.rm = TRUE when you want to exclude missing values from the calculation. This function pairs well with dplyr’s summarize() for clean, readable data analysis code.