How to find the mean based on single group value in an R data frame?


To find the mean based on single group value in an R data frame, we can use mean function with subsetting through single square brackets.

For example, if we have a data frame called df that contains a categorical column say C having three groups Low, Medium, High and a numerical column say Num then mean of Num for Medium group can be found by using the command given below −

mean(df$C[df$Num=="Medium"])

Example 1

Following snippet creates a sample data frame −

Group<-sample(LETTERS[1:4],20,replace=TRUE)
Score<-rpois(20,5)
df1<-data.frame(Group,Score)
df1

The following dataframe is created −

 Group Score
1  D   7
2  D   6
3  D   3
4  B   5
5  C   7
6  A   2
7  D   7
8  D   5
9  C   1
10 C   6
11 C   9
12 D   3
13 C   6
14 B   8
15 C   2
16 B   6
17 D   7
18 C   8
19 A  12
20 B   3

To get the mean of Score for group A, add the following code to the above snippet −

Group<-sample(LETTERS[1:4],20,replace=TRUE)
Score<-rpois(20,5)
df1<-data.frame(Group,Score)
mean(df1$Score[df1$Group=="A"])

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 7

Example 2

Following snippet creates a sample data frame −

Class<-sample(c("I","II","III"),20,replace=TRUE)
Price<-sample(10:20,20,replace=TRUE)
df2<-data.frame(Class,Price)
df2

The following dataframe is created −

 Class Price
1   II  19
2    I  15
3    I  14
4    I  19
5  III  13
6    I  17
7  III  12
8    I  10
9   II  18
10  II  11
11  II  13
12   I  11
13  II  12
14 III  11
15  II  13
16  II  15
17 III  10
18   I  17
19 III  18
20   I  14

To get the mean of Price for Class I, add the following code to the above snippet −

Class<-sample(c("I","II","III"),20,replace=TRUE)
Price<-sample(10:20,20,replace=TRUE)
df2<-data.frame(Class,Price)
mean(df2$Price[df2$Class=="I"])

Output

If you execute all the above given snippets as a single program, it generates the following output: −

[1] 14.625

Example 3

Following snippet creates a sample data frame −

Category<-sample(c("First","Second","Third","Fourth"),20,replace=TRUE)
Sales<-sample(10:50,20)
df3<-data.frame(Category,Sales)
df3

The following dataframe is created −

 Category Sales
1  First   49
2  Fourth  43
3  Second  44
4  Third   35
5  Fourth  21
6  Third   50
7  First   45
8  Fourth  15
9  Second  20
10 First   11
11 First   17
12 Fourth  37
13 Third   48
14 Second  32
15 Fourth  10
16 Fourth  38
17 First   40
18 First   22
19 Second  25
20 First   47

To get the mean of Sales for group First, add the following code to the above snippet −

Category<-sample(c("First","Second","Third","Fourth"),20,replace=TRUE)
Sales<-sample(10:50,20)
df3<-data.frame(Category,Sales)
mean(df3$Sales[df3$Category=="First"])

Output

If you execute all the above given snippets as a single program, it generates the following output: −

[1] 33

Updated on: 10-Nov-2021

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements