How to Sum Specific Columns in R (With Examples)

In R, you can easily sum specific columns of a dataframe or matrix using the rowSums() function. This function takes the dataframe or matrix as the first argument and the columns to be summed as additional arguments. You can also use the colSums() function to sum specific rows of a dataframe or matrix. Examples of both functions are provided in this tutorial.


Often you may want to find the sum of a specific set of columns in a data frame in R. Fortunately this is easy to do using the rowSums() function.

This tutorial shows several examples of how to use this function in practice.

Example 1: Find the Sum of Specific Columns

The following code shows how to create a data frame with three columns and find the sum of the first and third columns:

#create data frame
data <- data.frame(var1 = c(0, NA, 2, 2, 5),
                   var2 = c(5, 5, 7, 8, 9),
                   var3 = c(2, 7, 9, 9, 7))

#view data frame
data

  var1 var2 var3
1    0    5    2
2   NA    5    7
3    2    7    9
4    2    8    9
5    5    9    7

#find sum of first and third columns
rowSums(data[ , c(1,3)], na.rm=TRUE)

[1]  2  7 11 11 12

The way to interpret the output is as follows:

  • The sum of values in the first row for the first and third columns is 2.
  • The sum of values in the first row for the first and third columns is 7.
  • The sum of values in the first row for the first and third columns is 11.
  • The sum of values in the first row for the first and third columns is 11.
  • The sum of values in the first row for the first and third columns is 12.

You can also assign the row sums of these specific columns to a new variable in the data frame:

#assign row sums to new variable named row_sum
data$row_sum <- rowSums(data[ , c(1,3)], na.rm=TRUE)

#view data frame
data

  var1 var2 var3 row_sum
1    0    5    2       2
2   NA    5    7       7
3    2    7    9      11
4    2    8    9      11
5    5    9    7      12

Example 2: Find the Sum of All Columns

It’s also possible to find the sum across all columns in a data frame. The following code shows how to do so:

#find row sums across all columns
data$new <- rowSums(data, na.rm=TRUE)

#view data frame
data

  var1 var2 var3 new
1    0    5    2   7
2   NA    5    7  12
3    2    7    9  18
4    2    8    9  19
5    5    9    7  21

We can see that:

  • The sum of values in the first row across all three columns is 7.
  • The sum of values in the second row across all three columns is 12.

And so on.

You can find more R tutorials here.

x