Cumulative percentage of a column in R

Cumulative percentage of the column in R can be accomplished by using cumsum and sum function. In this tutorial we will see how to

  • Get cumulative percentage of the column in R

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 percentage of the column in R 1

 

Get cumulative percentage of column in R:

Cumulative percentage of a column is calculated using cumsum() and sum() function as shown below

# cumulative percentage

df1$cumulative_percentage= 100*cumsum(df1$Score)/sum(df1$Score)
df1

resultant dataframe will be,

cumulative percentage of the column in R 2

                                                                                                           

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.