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

Percentage of the column in R - image previous-small-11-1 on http://www.datasciencemadesimple.com                                                                                                              Percentage of the column in R - image next_small-11-1 on http://www.datasciencemadesimple.com