- 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 find the percentage of each category in an R data frame column?
To find the percentage of each category in an R data frame column, we can follow the below steps −
First of all, create a data frame.
Then, use summarise function of dplyr package after grouping along with n and nrow.
Example
Create the data frame
Let’s create a data frame as shown below −
Group<-sample(LETTERS[1:5],25,replace=TRUE) DV<-rpois(25,5) df<-data.frame(Group,DV) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Group DV 1 D 8 2 A 3 3 D 6 4 D 2 5 E 4 6 D 4 7 B 6 8 A 5 9 C 4 10 E 7 11 D 5 12 B 7 13 B 1 14 A 1 15 B 9 16 E 2 17 C 6 18 E 7 19 D 8 20 A 11 21 B 6 22 B 9 23 C 8 24 B 7 25 A 8
Find the percentage of each category in data frame
Using summarise function of dplyr package after grouping along with n and nrow to find the percentage of each category in Group column of data frame df −
Group<-sample(LETTERS[1:5],25,replace=TRUE) DV<-rpois(25,5) df<-data.frame(Group,DV) library(dplyr) df %>% group_by(Group) %>% summarise(Percentage=n()/nrow(.))
Output
# A tibble: 5 x 2 Group Percentage <chr> <dbl> 1 A 0.2 2 B 0.28 3 C 0.12 4 D 0.24 5 E 0.16
- Related Articles
- How to find the count of each category in an R data frame column?
- How to find the percentage of missing values in each column of an R data frame?
- How to find the percentage of zeros in each column of a data frame in R?
- How to find the percentage of each category in a data.table object column in R?
- How to find the number of NA’s in each column of an R data frame?
- How to find the number of zeros in each column of an R data frame?
- How to find the percentage of missing values in an R data frame?
- How to extract the closest value to a certain value in each category in an R data frame?
- How to find the percentage of values that lie within a range in R data frame column?
- Find the frequency of unique values for each column in an R data frame.
- How to add a new column to represent the percentage for groups in an R data frame?
- How to find the maximum of each row in an R data frame?
- How to find the frequency of each value in an R data frame?
- How to find the sum of column values of an R data frame?
- How to find mode for an R data frame column?

Advertisements