rowSums(): Calculating the Sum of Rows of a Matrix or Data Frame in R

The rowSums() function calculates the sum of values in each numeric row of a matrix, array, or data frame.

It quickly sums up data across rows without having to write loops, making it faster and more memory-efficient than manual iteration.

How to Use rowSums() function on data frame

In the above figure, we showed the sum of each row in a data frame using the rowSums() function. 

# Create a data frame.
df <- data.frame(
  col1 = c(1, 2, 3),
  col2 = c(4, 5, 6),
  col3 = c(7, 8, 9)
)

# Calculate the row sums.
rowSums(df)


# Output: [1] 12 15 18

The sum of the first row is 12.

The sum of the second row is 15.

The sum of the third row is 18.

Syntax

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

Parameters

Argument Description
x It represents the matrix or data frame.
na.rm It is a logical argument. If TRUE, NA values are ignored.

By default, NA values are not ignored, and you need to set it to False explicitly.

dims

It represents the dimensions of x to retain before summation.

By default, its value is 1, meaning the function operates over rows.

Handling NA values

If the row contains an NA value, the output will be NA because it does not know what NA means. So, it returns NA.

Figure of using rowSums() function with NA Values in Data Frame

# Create a data frame.
df <- data.frame(
  col1 = c(NA, 2, 3),
  col2 = c(4, NA, 6),
  col3 = c(7, 8, NA)
)

# Calculate the row sums.
rowSums(df)

# Output: [1] NA  NA  NA

To ignore the NA values in a data frame and calculate the sum of the remaining values of rows, pass the na.rm = TRUE.

Handling NA values

# Create a data frame.
df <- data.frame(
  col1 = c(NA, 2, 3),
  col2 = c(4, NA, 6),
  col3 = c(7, 8, NA)
)

# Calculate the row sums.
rowSums(df, na.rm = TRUE)

# Output: [1] 11  10  9

In the first row, it only adds 4 + 7 and ignores NA. So, the output is 11.

The same thing happened with the second row. It only counts 2 and 8, and their sum is 10.

Working with specific rows of a data frame

You can use indexing to select the specific rows for summation. Pass those selections to the rowSums() function. Let’s say I want the sum of row 1 and the sum of row 2.

Use the rowSums() with specific rows of a data frame

# Create a data frame.
df <- data.frame(
  col1 = c(NA, 2, 3),
  col2 = c(4, NA, 6),
  col3 = c(7, 8, NA)
)

# Calculate the row sums.
rowSums(df[c(1, 2), ], na.rm = TRUE)

# Output:
#  1    2
# 11   10

Matrix

In a matrix, there are rows and columns, and rowSums() only focus on rows. If you pass the whole matrix to this function, it will return a vector with the sum of each row.

Calculating the sum of rows of matrix

rv <- rep(1:9)

mtrx <- matrix(rv, 3, 3)
mtrx

cat("The sum of rows is: ", "\n")
rowSums(mtrx)

Use the rowSums() with matrix

For other types of row-wise operations (like concatenation of strings), you would use different functions or approaches.

Multi-Dimensional Array

Let’s take a multi-dimensional array as input and find the sum of rows based on dimensions.

arr <- array(1:24, dim = c(2, 3, 4))

rowSums(arr, dims = 1)

# Output: [1] 144 156

rowSums(arr, dims = 2)

# Output:
#      [,1]  [,2]  [,3]
# [1,]  40   48     56
# [2,]  44   52     60

In this code:

  1. dims=1: It is a 2×1 matrix with sums over the 3×4 plane per row.
  2. dims=2: It is a 2×3 matrix with sums over the 4 layers per row-column.

Empty input

What if the input matrix or data frame is empty? Well, in that case, it returns numeric(0) (empty numeric vector).

empty_vec <- matrix(numeric(0), nrow = 0, ncol = 3)

rowSums(empty_vec)

# Output: numeric(0)

Non-numeric columns

The rowSums() function can only operate on numeric columns. If your input data frame contains character columns, it will throw this error: ‘x’ must be numeric.

df <- data.frame(a = c(1, 2), b = c("A", "B")) # Mixed types

rowSums(df)

# Output:
# Error in rowSums(df) : 'x' must be numeric
# Execution halted

Ensure that the input rows are numeric to prevent this type of error, or convert to numeric if compatible.

That’s it!

Leave a Comment