colSums in R: How to Use colSums() Function in R

The rowSums() function calculates the sum of each row of a numeric array, matrix, or data frame. The colMeans() function calculates the mean of each column of a numeric array, matrix, or data frame. The rowMeans() function calculates the mean of each row of a numeric data frame, matrix, or array.

colSums in R

The colSums() is a built-in R function that calculates the sum of a column. The colSums() function takes an R object like an array of two or more dimensions and returns the sum of the columns.

The colSums() function calculates the sum of each column of a numeric data frame, matrix, or array. It forms the row and column sums and means for numeric arrays (or data frames).

Syntax

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

Parameters

x: It is an array of two or more dimensions, containing numeric, complex, integer, or logical values, or a numeric data frame.

na.rm: It is logical. Should missing values (including NaN) be omitted from the calculations.

dims: It is an integer: Which dimensions are regarded as ‘rows’ or ‘columns’ to sum over.

Example

To create a matrix in R, use the matrix() function. We will use the rep() function to create a vector and then use the matrix() function to create a matrix function.

mtrx <- matrix(rep(1:4), 2, 2)
mtrx

Output

     [,1] [,2]
[1,]   1    3
[2,]   2    4

Now, use the colSums() function to calculate the sum of column values of the matrix.

mtrx <- matrix(rep(1:4), 2, 2)
mtrx
cat("The sum of columns is: ", "\n")
colSums(mtrx)

Output

     [,1] [,2]
[1,]   1    3
[2,]   2    4

The sum of columns is:

[1] 3 7

Finding the sum of column values of the 3D array.

To create a 3D array, use the array() function. Then, use the colSums() function to calculate the sum of columns of the array.

arr <- array(1:12, c(2, 3, 3))
arr
cat("The sum of columns is: ", "\n")
colSums(arr)

Output

, , 1

     [,1] [,2] [,3]
[1,]   1    3    5
[2,]   2    4    6

, , 2

     [,1] [,2] [,3]
[1,]   7    9   11
[2,]   8   10   12

, , 3

      [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

The sum of columns is:
 
     [,1] [,2] [,3]
[1,]   3   15    3
[2,]   7   19    7 
[3,]  11   23   11

Calculating the sum of column values in R Data Set

You can use the inbuilt R dataset like ChickWeight and calculate the sum of its column values. But, first, let’s get the snapshot of the ChickWeight dataset using the head() function.

head(USArrests, 5)

Output

          Murder Assault UrbanPop  Rape
Alabama    13.2    236     58      21.2
Alaska     10.0    263     48      44.5
Arizona     8.1    294     80      31.0
Arkansas    8.8    190     50      19.5
California  9.0    276     91      40.6

We will use the colSums() function to calculate the sum of Murder, Assult, UrbanPop, and Rape column values.

colSums(USArrests)

Output

Murder  Assault  UrbanPop   Rape
389.4   8538.0    3277.0   1061.6

That is it for colSums() function in R.

See also

Median in R

Average in R

Calculate the square of Number in R

Leave a Comment