# How to find the subtotal in R?

R ProgrammingServer Side ProgrammingProgramming

By subtotal we mean finding the sum of values based on grouping column. For example, if we have a data frame called df that contains three numerical columns as x, y, z and one categorical column say Group then the subtotal of x, y, z for each category in Group can be found by using the command aggregate(cbind(x,y,z)~Group,data=df,FUN=sum).

Consider the below data frame −

## Example

Live Demo

x1<-rpois(20,2)
x2<-rpois(20,2)
x3<-rpois(20,2)
Grp<-sample(c("I","II","III"),20,replace=TRUE)
df1<-data.frame(x1,x2,x3,Grp)
df1

## Output

  x1 x2  x3  Grp
1  8  1  2    I
2  2  2  2  III
3  1  2  1   II
4  1  3  1   II
5  3  0  2   II
6  3  2  2  III
7  2  1  1  III
8  2  1  4  III
9  4  3  3    I
10 0  0  3   II
11 1  4  1    I
12 2  2  7   III
13 3  1  0    II
14 3  5  2    II
15 3  2  5   III
16 0  0  2    II
17 0  3  0   III
18 5  5  1    II
19 3  3  2    II
20 2  3  2     I

Finding the subtotal for categories in Grp −

## Example

aggregate(cbind(x1,x2,x3)~Grp,data=df1,FUN=sum)

## Output

  Grp x1  x2  x3
1   I  15  11   8
2  II  19  19  14
3 III  14  13  21

## Example

Live Demo

Category<-sample(c("First","Second","Third","Fourth"),20,replace=TRUE)
y1<-rpois(20,12)
y2<-rpois(20,12)
y3<-rpois(20,12)
y4<-rpois(20,12)
df2<-data.frame(Category,y1,y2,y3,y4)
df2

## Output

   Category y1  y2   y3  y4
1  Second   14  15  16   12
2  Fourth   12  20  11   19
3  Fourth   18  8   15   10
4  First    12  7   14   11
5  First    5   17  15   16
6  First    15  10  9    12
7  Fourth   8  15   8    10
8  Third    17 15   14   11
9  Second   11  12  10   15
10 First    16  18  14   6
11 Fourth   14  11   10   12
12 Third    15  17   13   22
13 First    18  19   9     7
14 Second   13  16   16   10
15 Fourth   6   10   21   20
16 Fourth   8   12   14   13
17 Third    17  16   16   13
18 Second   8   11   14    9
19 First    10  9   16    10
20 Second   3   10   9    12

Finding the subtotal for categories in Category column of df2 −

## Example

aggregate(cbind(y1,y2,y3,y4)~Category,data=df2,FUN=sum)

## Output

   Category  y1  y2  y3 y4
1  First    76  80  77  62
2  Fourth   66  76  79  84
3  Second   49  64  65  58
4  Third    49  48  43  46
Published on 11-Feb-2021 11:48:32