Cumulative product of a column in R

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

  • Get cumulative product of column using cumprod() method
  • 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

 

                                                                                                           

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.