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
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
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