How to find the groupwise mean and save it in a data frame object in R?

R ProgrammingServer Side ProgrammingProgramming

We often need groupwise mean in data analysis, especially in situations where analysis of variance techniques is used because these techniques helps us to compare different groups based on their measures of central tendencies and measures of variations. It can be done by using aggregate function so that the output can be saved in a data frame object. In the below examples, we can see how it can be done and also check the final object type.

Example

Consider the below data frame −

 Live Demo

set.seed(109)
Salary<-sample(10000:20000,20)
Group<-sample(c("High-school","Graduate","Post-Graduate"),20,replace=TRUE)
df1<-data.frame(Group,Salary)
df1

Output

       Group      Salary
1    Graduate      10250
2  High-school     14923
3  High-school     18928
4  High-school     19800
5  Graduate        15974
6   High-school    16270
7  Post-Graduate   19832
8 Graduate         19246
9   Graduate       11699
10  Graduate       17424
11  High-school    14875
12 Post-Graduate   12319
13 Post-Graduate   16900
14 High-school     12361
15 Post-Graduate   15809
16 Post-Graduate   19854
17 High-school   14387
18 High-school   13647
19 Graduate   18587
20 Graduate   11817

Finding the groupwise mean with aggregate function −

Groupwise_mean<-aggregate(df1$Salary,list(df1$Group),mean)
Groupwise_mean
Group.1 x
1 Graduate 14924.60
2 High-school 16524.57
3 Post-Graduate 17362.67

Checking whether the object Groupwise mean is a data frame or not −

is.data.frame(Groupwise_mean)
[1] TRUE

Let’s have a look at another example −

Example

 Live Demo

Class<-rep(LETTERS[1:4],times=5)
Age<-sample(19:30,20,replace=TRUE)
df2<-data.frame(Class,Age)
df2

Output

  Class  Age
1  A     29
2  B     22
3  C     26
4  D     20
5  A     28
6  B     21
7  C     19
8  D     24
9  A     29
10 B     30
11 C     23
12 D     25
13 A     21
14 B     21
15 C     21
16 D     20
17 A     21
18 B     24
19 C     19
20 D     21

> Groupwise_mean_Age<-aggregate(df2$Age,list(df2$Class),mean) 

> Groupwise_mean_Age 

Group.1 x 

Group.1x
1 A 24.8 
2 B 25.2 
3 C 25.2 
4 D 24.0 
> is.data.frame(Groupwise_mean_Age) [1] TRUE
raja
Updated on 07-Oct-2020 16:16:25

Advertisements