• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to get product of all elements in a column

rstats101 · July 1, 2024 ·

In this tutorial we will learn how to compute product of all elements of a column in a dataframe using tidyverse. We will use prod() function in base to multiple all elements of a columns.

Let us first load tidyverse.

library(tidyverse)

We will create a simple dataframe using tidyverse’ tibble() with a column containing numbers from 1 to 6.

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

# A tibble: 6 × 1
     id
  <int>
1     1
2     2
3     3
4     4
5     5
6     6

Here we use prod() function on the column of interest to compute the product of all elements in the column. We use summarize() function from dplyr to save the result of multiplying all the elements of a column as a dataframe.

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

# A tibble: 1 × 1
  product
    <dbl>
1     720

Note that, by default prod() function does not remove NAs. Let us see an example of how to handle NAs using prod() function. First let us create column with one or more NAs.

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

# A tibble: 5 × 1
     id
  <int>
1     3
2     1
3     4
4    NA
5     5

Therefore, if you have a column with one or more missing values NAs, we will get NA as a result.

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

# A tibble: 1 × 1
  product
    <dbl>
1      NA

To remove NAs and perform multiplication of all non-NA elements in a column, we should specify na.rm=TRUE as argument to prod() function. Now we get the expected answer multiplying all non-NAs.

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

# A tibble: 1 × 1
  product
    <dbl>
1      60

Related

Filed Under: prod(), R Function, rstats101 Tagged With: product of all elements

Reader Interactions

Trackbacks

  1. How to compute annualized return of a stock with tidyverse - Rstats 101 says:
    July 2, 2024 at 8:02 am

    […] return for each year. We can use the formula for annualized return shown above and compute using prod() function as shown […]

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version