How to find the row wise sum for n number of columns in R?


To find the row wise sum of n number of columns can be found by using the rowSums function along with subsetting of the columns with single square brackets.

For example, if we have a data frame called df that contains five columns and we want to find the row sums for last three columns then we can use the following command −

df$Sum_3<-rowSums(df[,3:5])

Example 1

Following snippet creates a sample data frame −

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

The following dataframe is created −

    x1     x2    x3
1  -0.66  1.01  -0.44
2  -2.05 -0.47   0.42
3   1.11  1.47   1.04
4  -2.55 -0.42  -0.46
5  -0.50 -1.98  -0.41
6  -2.16 -0.34  -0.55
7   0.29 -0.58   0.42
8  -0.67  1.71  -0.86
9  -0.93  0.00   0.00
10  0.30 -0.87   0.27
11  1.27 -1.60  -0.49
12  0.73  1.16  -1.24
13 -0.71  0.20  -1.18
14 -0.31  1.16   0.24
15 -0.76  0.86  -0.70
16  1.11 -0.25  -0.48
17 -0.88 -0.43   0.10
18  0.50 -2.05   0.36
19 -0.30  0.56  -0.97
20 -1.76 -1.39   1.10

To find the row sums for column x2 and x3, add the following code to the above snippet −

x1<-round(rnorm(20),2)
x2<-round(rnorm(20),2)
x3<-round(rnorm(20),2)
df1<-data.frame(x1,x2,x3)
df1$Sum_x2_x3<-rowSums(df1[,2:3])
df1

Output

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

     x1    x2    x3   Sum_x2_x3
1  -0.66  1.01 -0.44  0.57
2  -2.05 -0.47  0.42 -0.05
3   1.11  1.47  1.04  2.51
4  -2.55 -0.42 -0.46 -0.88
5  -0.50 -1.98 -0.41 -2.39
6  -2.16 -0.34 -0.55 -0.89
7   0.29 -0.58  0.42 -0.16
8  -0.67  1.71 -0.86  0.85
9  -0.93  0.00  0.00  0.00
10  0.30 -0.87  0.27 -0.60
11  1.27 -1.60 -0.49 -2.09
12  0.73  1.16 -1.24 -0.08
13 -0.71  0.20 -1.18 -0.98
14 -0.31  1.16  0.24  1.40
15 -0.76  0.86 -0.70  0.16
16  1.11 -0.25 -0.48 -0.73
17 -0.88 -0.43  0.10 -0.33
18  0.50 -2.05  0.36 -1.69
19 -0.30  0.56 -0.97 -0.41
20 -1.76 -1.39  1.10 -0.29

Example 2

Following snippet creates a sample data frame −

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

The following dataframe is created −

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

To find row sums for column y1 and y2, add the following code to the above snippet −

y1<-rpois(20,1)
y2<-rpois(20,5)
y3<-rpois(20,2)
y4<-rpois(20,2)
df2<-data.frame(y1,y2,y3,y4)
df2$Sum_y1_y2<-rowSums(df2[,1:2])
df2

Output

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

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

Updated on: 23-Nov-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements