Cumulative product of a column in R

Cumulative product of the column in R can be accomplished by using cumprod function. In this tutorial we will see how to

  • Get cumulative product of column
  • Reverse cumulative product of column

Let’s first create the dataframe

df1 = data.frame(State = c('Arizona AZ','Georgia GG', 'Newyork NY','Indiana IN','Florida FL'), Score=c(62,47,55,74,31))
df1

df1 will be

Cumulative product of column in r 1

 

Get cumulative product of column in R

Cumulative product of a column is calculated using cumprod() function as shown below

# cumulative product

df1$cumulative_prod = cumprod(df1$Score)
df1

resultant dataframe will be

Cumulative product of column in r 2

 

Get Reverse cumulative product of column in R

Reverse cumulative product of a column is calculated using rev() and cumprod() function as shown below

# reverse cumulative product

df1$rev_cumulative_prod = rev(cumprod(df1$Score))
df1

df1 will be

Cumulative product of column in r 3

Cumulative product of a column in R - image previous-small-11-1 on http://www.datasciencemadesimple.com                                                                                                                Cumulative product of a column in R - image next_small-11-1 on http://www.datasciencemadesimple.com