Home » How to Sum Specific Columns in R (With Examples)

How to Sum Specific Columns in R (With Examples)

by Tutor Aspire

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 #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 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 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.

You may also like