How to find the sum of rows of a column based on multiple columns in R data frame?

R ProgrammingServer Side ProgrammingProgramming

To find the sum of rows of a column based on multiple columns in R data frame, we can follow the below steps −

  • First of all, create a data frame.

  • Then, use aggregate function to find the sum of rows of a column based on multiple columns.

Example

Create the data frame

Let’s create a data frame as shown below −

Grp1<-sample(LETTERS[1:4],25,replace=TRUE)
Grp2<-sample(letters[1:4],25,replace=TRUE)
Grp3<-sample(c("A-1","B-2","C-3"),25,replace=TRUE)
DV<-sample(1:50,25)
df<-data.frame(Grp1,Grp2,Grp3,DV)
df

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   Grp1 Grp2 Grp3 DV
1  D    b    C-3  35
2  D    c    B-2   4
3  A    a    C-3  45
4  A    b    C-3   7
5  C    b    C-3  21
6  A    b    C-3  31
7  A    c    B-2  37
8  A    b    C-3   5
9  D    b    B-2  36
10 B    d    A-1  41
11 B    c    C-3   3
12 B    b    C-3  14
13 A    b    C-3  27
14 D    b    B-2  15
15 B    d    C-3  47
16 B    c    C-3  18
17 C    a    C-3   6
18 A    b    B-2  10
19 A    c    A-1  49
20 A    d    C-3  16
21 A    a    C-3  40
22 D    b    B-2  29
23 A    c    B-2  17
24 D    b    A-1  19
25 B    c    B-2  50

Find the sum of rows of a column based on multiple columns

Using aggregate function to find the sum of rows of column DV based on columns Grp1, Grp2, and Grp3 as shown below −

Grp1<-sample(LETTERS[1:4],25,replace=TRUE)
Grp2<-sample(letters[1:4],25,replace=TRUE)
Grp3<-sample(c("A-1","B-2","C-3"),25,replace=TRUE)
DV<-sample(1:50,25)
df<-data.frame(Grp1,Grp2,Grp3,DV)
aggregate(DV~Grp1+Grp2+Grp3,data=df,FUN=sum)

Output

   Grp1 Grp2 Grp3 DV
1  D    b    A-1  19
2  A    c    A-1  49
3  B    d    A-1  41
4  A    b    B-2  10
5  D    b    B-2  80
6  A    c    B-2  54
7  B    c    B-2  50
8  D    c    B-2   4
9  A    a    C-3  85
10 C    a    C-3   6
11 A    b    C-3  70
12 B    b    C-3  14
13 C    b    C-3  21
14 D    b    C-3  35
15 B    c    C-3  21
16 A    d    C-3  16
17 B    d    C-3  47
raja
Updated on 08-Nov-2021 09:55:05

Advertisements