- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add a new column to represent the percentage for groups in an R data frame?
In data analysis, we often need to find the percentage of values that exists in a data group. This helps us to understand which value occurs frequently and which one has low frequency. Also, plotting of percentages through pie charts can be done and that gives a better view of the data to the readers. Adding a new column as percentage for groups is not a challenge if we can use mutate function of dplyr package, here you will get the examples from that.
Example1
> Group<-rep(1:2,each=5) > Frequency<-sample(1:100,10) > df1<-data.frame(Group,Frequency) > df1
Output
Group Frequency 1 1 67 2 1 58 3 1 54 4 1 13 5 1 23 6 2 91 7 2 3 8 2 95 9 2 38 10 2 48
> library(dplyr)
Finding the percentage for each group values in the group −
> df1%>%group_by(Group)%>%mutate(Percentage=paste0(round(Frequency/sum(Frequency)*100,2),"%")) # A tibble: 10 x 3 # Groups: Group [2]
Output
Group Frequency Percentage <int> <int> <chr> 1 1 67 31.16% 2 1 58 26.98% 3 1 54 25.12% 4 1 13 6.05% 5 1 23 10.7% 6 2 91 33.09% 7 2 3 1.09% 8 2 95 34.55% 9 2 38 13.82% 10 2 48 17.45%
Example2
> Gender<-rep(c("Male","Female"),each=5) > Salary<-sample(25000:50000,10) > df2<-data.frame(Gender,Salary) > df2
Output
Gender Salary 1 Male 41734 2 Male 39035 3 Male 36161 4 Male 33437 5 Male 45123 6 Female 44492 7 Female 48456 8 Female 31569 9 Female 35110 10 Female 43630
>df2%>%group_by(Gender)%>%mutate(Percentage=paste0(round(Salary/sum(Salary)*1 00,2),"%")) # A tibble: 10 x 3 # Groups: Gender [2]
Output
Gender Salary Percentage <fct> <int> <chr> 1 Male 41734 21.35% 2 Male 39035 19.97% 3 Male 36161 18.5% 4 Male 33437 17.1% 5 Male 45123 23.08% 6 Female 44492 21.89% 7 Female 48456 23.84% 8 Female 31569 15.53% 9 Female 35110 17.27% 10 Female 43630 21.47%
Example3
> Grade<-rep(c("A","B","C","D","E"),each=2) > Number_of_Years_in_Job<-sample(1:5,10,replace=TRUE) > df3<-data.frame(Grade,Number_of_Years_in_Job) > df3
Output
Grade Number_of_Years_in_Job 1 A 4 2 A 5 3 B 4 4 B 4 5 C 1 6 C 4 7 D 1 8 D 1 9 E 3 10 E 1
>df3%>%group_by(Grade)%>%mutate(Percentage=paste0(round(Number_of_Years_in_J ob/sum(Number_of_Years_in_Job)*100,2),"%")) # A tibble: 10 x 3 # Groups: Grade [5]
Output
Grade Number_of_Years_in_Job Percentage <fct> <int> <chr> 1 A 4 44.44% 2 A 5 55.56% 3 B 4 50% 4 B 4 50% 5 C 1 20% 6 C 4 80% 7 D 1 50% 8 D 1 50% 9 E 3 75% 10 E 1 25%
- Related Articles
- How to add a new column at the front of an existing R data frame?
- How to add a new column to a data frame using mutate in R?
- How to add a new column in an R data frame with count based on factor column?
- How to add a new column to an R data frame with largest value in each row?
- How to find the percentage of each category in an R data frame column?
- How to find percentile rank for groups in an R data frame?
- How to concatenate column values and create a new column in an R data frame?
- How to add a column in an R data frame with consecutive numbers?
- How to add a new column in an R data frame by combining two columns with a special character?
- How to add single quotes to strings in an R data frame column?
- How to convert an old data frame to new data frame in R?
- How to find the percentage of missing values in each column of an R data frame?
- How to find mode for an R data frame column?
- How to add a column between columns or after last column in an R data frame?
- How to find the percentage of zeros in each column of a data frame in R?

Advertisements