How do I sum specific columns in R with examples?

In R, the sum() function can be used to sum specific columns in a dataset. This function takes the columns as arguments and returns the sum of the values in those columns. For example, to sum the “Age” and “Income” columns in a dataset called “customers”, the code would be:

sum(customers$Age, customers$Income)

This will return the total sum of the “Age” and “Income” columns. Additionally, you can also use the colSums() function to sum multiple columns at once. This function takes the dataset as the argument and returns the sum of each column. For instance, to sum all numerical columns in the “customers” dataset, the code would be:

colSums(customers)

Overall, using the sum() or colSums() function can effectively help in calculating the total sum of specific columns in a dataset in R.

Sum Specific Columns in R (With Examples)


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 columnsdata$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