How to create a row at the end an R data frame with column totals?


In data analysis, we often need column totals, especially in situations where we want to perform the analysis in a step by step manner. There are many analytical techniques in which we find the column totals such as ANALYSIS OF VARIANCE, CORRELATION, REGRESSION, etc. To find the column totals, we can use colSums function and use the single square brackets to put these totals as a row in the data frame.

Example1

Consider the below data frame −

 Live Demo

> x1<-1:20
> x2<-1:20
> x3<-1:20
> df1<-data.frame(x1,x2,x3)
> df1

Output

  x1 x2 x3
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
11 11 11 11
12 12 12 12
13 13 13 13
14 14 14 14
15 15 15 15
16 16 16 16
17 17 17 17
18 18 18 18
19 19 19 19
20 20 20 20
> df1["Total",]<-colSums(df1)
> df1

Output

 x1 x2 x3
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
11 11 11 11
12 12 12 12
13 13 13 13
14 14 14 14
15 15 15 15
16 16 16 16
17 17 17 17
18 18 18 18
19 19 19 19
20 20 20 20
Total 210 210 210

Example2

 Live Demo

> x1<-rpois(20,1)
> x2<-rpois(20,2)
> x3<-rpois(20,5)
> df2<-data.frame(x1,x2,x3)
> df2

Output

 x1 x2 x3
1 0 4 3
2 3 2 1
3 1 4 7
4 1 3 5
5 1 3 7
6 1 0 6
7 0 0 7
8 1 2 5
9 0 1 7
10 3 3 6
11 0 1 5
12 2 1 6
13 0 0 7
14 0 2 3
15 0 4 3
16 1 1 3
17 2 3 6
18 1 2 5
19 1 3 4
20 0 2 1
> df2["Total",]<-colSums(df2)
> df2

Output

 x1 x2 x3
1 0 4 3
2 3 2 1
3 1 4 7
4 1 3 5
5 1 3 7
6 1 0 6
7 0 0 7
8 1 2 5
9 0 1 7
10 3 3 6
11 0 1 5
12 2 1 6
13 0 0 7
14 0 2 3
15 0 4 3
16 1 1 3
17 2 3 6
18 1 2 5
19 1 3 4
20 0 2 1
Total 18 41 97

Example3

 Live Demo

> x1<-rnorm(20,0.5)
> x2<-rnorm(20,1.5)
> x3<-rnorm(20,2.5)
> df3<-data.frame(x1,x2,x3)
> df3

Output

   x1          x2       x3
1 0.6164833 0.47429064 3.5166292
2 2.0596947 1.10363170 3.4169209
3 1.5354324 1.96449893 1.7139730
4 -0.3155407 0.06443867 4.0183405
5 1.0863162 1.85855640 1.8751935
6 1.4546097 2.27657919 1.6122213
7 2.0087382 1.74009432 2.5015685
8 -0.3410458 2.41762264 2.9820183
9 -0.2868343 1.13547227 4.3164365
10 -1.0235788 2.14507250 2.3995348
11 0.2634310 1.63758312 2.3744627
12 0.9245307 -1.12596690 1.5528442
13 0.6475464 3.60709659 3.4380703
14 0.6304414 0.30028737 3.5130523
15 -0.8681919 2.16587601 0.8144658
16 0.1540673 2.11388876 2.0729619
17 2.6927877 2.37447334 2.9837406
18 -0.9019373 1.60907910 3.6548412
19 -0.2584275 1.04103727 0.7283439
20 0.8461264 0.85496302 3.2411674
> df3["Total",]<-colSums(df3)
> df3

Output

      x1       x2       x3
1 0.6164833 0.47429064 3.5166292
2 2.0596947 1.10363170 3.4169209
3 1.5354324 1.96449893 1.7139730
4 -0.3155407 0.06443867 4.0183405
5 1.0863162 1.85855640 1.8751935
6 1.4546097 2.27657919 1.6122213
7 2.0087382 1.74009432 2.5015685
8 -0.3410458 2.41762264 2.9820183
9 -0.2868343 1.13547227 4.3164365
10 -1.0235788 2.14507250 2.3995348
11 0.2634310 1.63758312 2.3744627
12 0.9245307 -1.12596690 1.5528442
13 0.6475464 3.60709659 3.4380703
14 0.6304414 0.30028737 3.5130523
15 -0.8681919 2.16587601 0.8144658
16 0.1540673 2.11388876 2.0729619
17 2.6927877 2.37447334 2.9837406
18 -0.9019373 1.60907910 3.6548412
19 -0.2584275 1.04103727 0.7283439
20 0.8461264 0.85496302 3.2411674
Total 10.9246490 29.75857494 52.7267868

Updated on: 04-Sep-2020

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements