# colSums function in R: Calculate the sum of each column in an R object

In this tutorial, we will show you what the colSums function in R does and how to use the colSums function to calculate the sum of each column in an R object. Follow us to learn more about the colSums() function with the explanation and examples below.

## ColSums Function In R

### What does the colSums() function do in R?

The first thing you should pay attention to when using the colSums() function is capitalizing the first ‘S’ character. The colSums() function in R is used to calculate the sum of each column in an R object such as: a 2D-matrix, a 3D matrix, or a data frame. Let’s take a look at the syntax of the colSums() function.

### Syntax

colSums (x, na.rm = FALSE, dims = 1)

Parameters

• x: The numeric R object which has two or more dimensions.
• na.rm: The default is FALSE. Omit the missing values or not.
• dims: The default is 1. What dimensions are considered ‘columns’ for the summation

After learning the usage and the syntax of the colSums() function in R, you will learn how to apply this function in the next title below.

## How To Use The colSums Function In R

Note that the colSums() function is only used with an object which has two or more dimensions like the matrix or the data frame.

### Calculate the sum of each column in the matrix

You can calculate the sum of each column in a matrix by the colSums() function.

Look at the example below.

# Create a matrix
mat <- matrix(1:15, nrow = 3, ncol = 5)
mat

Output

   [,1] [,2] [,3] [,4] [,5]
[1,]  1   4    7    10   13
[2,]  2   5    8    11   14
[3,]  3   6    9    12   15

Calculate the sum of each column in this matrix.

colSums(mat)

Output

[1] 6 15 24 33 42

This function will return the numeric vector of each column’s sum.

### Calculate the sum of each column in the data frame

You can calculate the sum of each column in the data frame by the colSums() function.

Look at the example below.

# Create the data frame
df <- data.frame(
Price = c(110, 140, 145, 95, 89, 66),
Surcharge = c(5, 10, 5.5, 10, 3.5, 7)
)

df
cat("Sum of each column: \n")

# Calculate each column's sum of this data frame
colSums(df)

Output

  Price Surcharge
1   110       5.0
2   140      10.0
3   145       5.5
4    95      10.0
5    89       3.5
6    66       7.0
Sum of each column:
Price Surcharge
645        41 

### Handle the missing values

By default, if a column only has one missing value, the sum of this column will be NA. To ignore the missing value, you can assign TRUE to the ‘na.rm’ parameter of the colSums() function.

Look at the example below.

# Create the data frame
df <- data.frame(
Price = c(110, 140, 145, 95, 89, 66),
Surcharge = c(5, NA, NA, 10, 3.5, 7)
)

# Calculate each column's sum of this data frame
colSums(df, na.rm = TRUE)

Output

    Price Surcharge
645.0      25.5 

Besides, click here to learn how to find the sum of a numeric object’s cumulative value.

## Summary

We have shown you the usage, the syntax, and how to apply the colSums function in R. By the colSums() function, you can calculate the sum of each column in an R object which has two or more dimensions. We hope this tutorial is helpful to you. Thanks!

Posted in R