How to find the percentage of each category in an R data frame column?

R ProgrammingServer Side ProgrammingProgramming

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
raja
Published on 16-Nov-2021 05:29:35
Advertisements