How to find the groupwise mean and groupwise sum at the same time in an R data frame?


To find the groupwise mean and groupwise sum at the same time, we can first convert the data frame into a data.table object and then apply the sum function and mean function for data.table object groups as shown in the below examples.

Example 1

Following snippet creates a sample data frame −

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

The following dataframe is created −

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

To load data.table package, convert df1 into a data.table object and find the groupwise mean and groupwise sum, add the following code to the above snippet −;

library(data.table)
DT1<-data.table(df1)
DT1[,list(Rate=sum(Rate),Score=mean(Score)),by="Group"]

Output

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

  Group Rate Score
1:  B   13  2.750000
2:  A    8  3.000000
3:  D   45  2.625000
4:  C   28  1.571429

Example 2

Following snippet creates a sample data frame −

Class<-sample(c("First","Second","Third"),20,replace=TRUE)
Price<-sample(200:1000,20)
Sales<-sample(50:100,20)
df2<-data.frame(Class,Price,Sales)
df2

The following dataframe is created −

   Class   Price Sales
1  Third   494   89
2  Second  222   90
3  First   466   53
4  First   425   94
5  First   567   66
6  Second  526   95
7  Third   443   97
8  Third   614   96
9  First   605   50
10 First   590  100
11 Second  853   92
12 First   818   73
13 Third   936   63
14 Second  932   68
15 First   677   88
16 First   851   80
17 Second  395   65
18 First   519   70
19 Second  804   77
20 Second  420   84

To convert df2 into a data.table object and find the groupwise mean and groupwise sum, add the following code to the above snippet −

DT2<-data.table(df2)
DT2[,list(Sales=sum(Sales),Price=mean(Price)),by="Class"]

Output

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

   Class   Sales  Price
1: Third   345  621.7500
2: Second  571  593.1429
3: First   674  613.1111

Updated on: 09-Nov-2021

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements