Percentage of the column in R

Percentage of the column in R is calculated in roundabout way using sum function. Let’s see how to calculate

  • Percentage of the column in R with example

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

So the resultant dataframe will be

percentage of the column in R 1

 

Percentage of the column in R:

Percentage of the column is calculated in roundabout way using sum function as shown below

df1$score_percen= 100*(df1$Score/sum(df1$Score))
df1

so the resultant dataframe will be

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.