How to add all columns of an R data frame by row?


To add all columns by row, we can use rowSums function.

For example, if we have a data frame called df that contains five columns say x, y, z, a, and b and we want to add all these columns by row then we can use the below mentioned command −

df$Total_sum<-rowSums(df[,c("x","y","z","a","b")])

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created −

       x1          x2           x3
1  -0.71576837   1.93636693   0.03572217
2   0.10588952  -0.18761810  -0.51570157
3   0.71766454  -0.48477796  -0.45083675
4  -0.51649822  -0.04961815  -0.50535820
5  -0.14545158   0.40937268   1.46923050
6   0.89138166  -2.27465679   0.07499376
7  -0.33328696   0.29308439  -1.69285367
8   0.23366842   0.23843342   0.81551733
9  -0.39147373  -0.91625212   0.34213384
10  0.02346723  -0.18371169   1.34624911
11 -0.88525152  -1.58919170  -1.79642822
12 -0.58824620   1.01577620   0.97343742
13 -0.11139240   0.34465454  -0.57030662
14  0.01699374   1.36414048   1.12037818
15  1.02739093   0.43833995   0.35741454
16 -0.05210225   -0.89179920 -0.53029706
17  0.11993986  -0.93023225   1.58035136
18  0.88157577  -0.68255508  -0.23090706
19  1.72864721   0.61535225   1.11270514
20  1.09912321   0.47399401  -0.22800965

To add all columns of df1 by row, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
df1$Total<-rowSums(df1[,c("x1","x2","x3")])
df1

Output

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

        x1           x2          x3         Total
1  -0.71576837   1.93636693   0.03572217   1.25632074
2   0.10588952  -0.18761810  -0.51570157  -0.59743015
3   0.71766454  -0.48477796  -0.45083675  -0.21795017
4  -0.51649822  -0.04961815  -0.50535820  -1.07147457
5  -0.14545158  0.40937268    1.46923050   1.73315160
6   0.89138166  -2.27465679   0.07499376  -1.30828137
7  -0.33328696   0.29308439  -1.69285367  -1.73305624
8   0.23366842   0.23843342   0.81551733   1.28761916
9  -0.39147373  -0.91625212   0.34213384  -0.96559201
10  0.02346723  -0.18371169   1.34624911   1.18600466
11 -0.88525152  -1.58919170  -1.79642822  -4.27087145
12 -0.58824620   1.01577620   0.97343742   1.40096742
13 -0.11139240   0.34465454  -0.57030662  -0.33704449
14  0.01699374   1.36414048   1.12037818   2.50151240
15  1.02739093   0.43833995   0.35741454   1.82314543
16 -0.05210225  -0.89179920  -0.53029706  -1.47419851
17  0.11993986  -0.93023225   1.58035136   0.77005896
18  0.88157577  -0.68255508  -0.23090706  -0.03188636
19  1.72864721   0.61535225   1.11270514   3.45670459
20  1.09912321   0.47399401  -0.22800965   1.34510757

Example 2

Following snippet creates a sample data frame −

y1<-rpois(20,1)
y2<-rpois(20,1)
y3<-rpois(20,1)
y4<-rpois(20,1)
df2<-data.frame(y1,y2,y3,y4)
df2

The following dataframe is created −

   y1 y2 y3 y4
1  2  0  1  0
2  0  2  2  1
3  1  1  0  1
4  1  1  1  0
5  0  1  0  0
6  2  1  3  5
7  0  2  0  0
8  0  3  0  2
9  0  0  1  1
10 2  0  1  2
11 1  1  1  3
12 0  1  1  1
13 0  0  1  1
14 1  0  2  1
15 0  1  0  3
16 0  0  0  0
17 1  2  2  1
18 1  2  1  1
19 1  1  1  2
20 1  1  0  1

To add all columns of df2 by row, add the following code to the above snippet −

y1<-rpois(20,1)
y2<-rpois(20,1)
y3<-rpois(20,1)
y4<-rpois(20,1)
df2<-data.frame(y1,y2,y3,y4)
df2$Total_Sum<-rowSums(df2[,c("y1","y2","y3","y4")])
df2

Output

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

   y1 y2 y3 y4 Total_Sum
1  2  0  1  0   3
2  0  2  2  1   5
3  1  1  0  1   3
4  1  1  1  0   3
5  0  1  0  0   1
6  2  1  3  5  11
7  0  2  0  0   2
8  0  3  0  2   5
9  0  0  1  1   2
10 2  0  1  2   5
11 1  1  1  3   6
12 0  1  1  1   3
13 0  0  1  1   2
14 1  0  2  1   4
15 0  1  0  3   4
16 0  0  0  0   0
17 1  2  2  1   6
18 1  2  1  1   5
19 1  1  1  2   5
20 1  1  0  1   3

Updated on: 12-Nov-2021

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements