Open In App

Compute the Sum of Rows of a Matrix or Array in R Programming – rowSums Function

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

rowSums() function in R Programming Language is used to compute the sum of rows of a matrix or an array. It simplifies the process of calculating the sum for each row, especially when dealing with large datasets, and can be applied to both numeric and logical data types.

Syntax:

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

Parameters:

  • x: array or matrix
  • dims: Integer: Dimensions are regarded as ‘rows’ to sum over. It is over dimensions dims+1,…

Example 1: Compute the Sum of Rows of a Matrix in R Programming

We will create one simple matrix and with the help of rowsum function, we will calculate the sums of the rows of the matrix.

R
x <- matrix(rep(2:10), 3, 3) 

return(x) 
print("Sum of the rows are:")
rowSums(x)

Output:

rowsum1

Row sum across 1-D array

Example 2: Compute the Sum of Rows of in a 3-Dimensional Array

We are creating a 3D array with numbers 1 to 8. Then, we are using rowSums() with dims = 1 to sum all the values in each row across all columns and depths.

R
x <- array(1:8, c(2, 2, 2)) 

print(x) 

rowSums(x, dims = 1) 

Output:

rowsum2

Row sum in a 3-D array

Example 3: Computing Row Sums in a Data Frame in R

We are creating a data frame data with three columns: ID, Val1, and Val2. We then calculate the row sums of the Val1 and Val2 columns using rowSums(), which adds the values of these columns for each row. Finally, we print the calculated row sums.

R
data <- data.frame(
  ID = c(1, 2, 3),
  Val1 = c(10, 20, 30),
  Val2 = c(5, 15, 25)
)

print(data)

row_sums <- rowSums(data[, c("Val1", "Val2")])

print(row_sums)

Output:

rowsum3

Row sum in a dataframe

Example 4: Computing Row Sums with Missing Values in a Data Frame in R

We are creating a data frame data with missing values (NA) in the Val1 and Val2 columns. The rowSums() function is used to calculate the sum of the rows, and the na.rm = TRUE argument is included to ignore missing values (NA) during the summation.

R
data <- data.frame(
  ID = c(1, 2, 3),
  Val1 = c(10, NA, 30),
  Val2 = c(5, 15, NA)
)

print("Original Data Frame with Missing Values:")
print(data)

row_sums <- rowSums(data[, c("Val1", "Val2")], na.rm = TRUE)
row_sums

Output:

rowsum4

Row sum in a datarfame with missing values

Example 5: Computing Row Sums with Selected Columns in R

We are creating a data frame data with multiple columns and missing values (NA) in Val1, Val2, val3, and val4. The rowSums() function is used to calculate the sum of Val2 and val4 for each row, while the na.rm = TRUE argument ensures that missing values (NA) are ignored during the summation.

R
data <- data.frame(
  ID = c(1, 2, 3),
  Val1 = c(10, NA, 30),
  Val2 = c(5, 15, NA),
  val3 = c(NA, 20, 5),
  val4 = c(15, 25, NA)
)

print("Original Data Frame with Missing Values:")
print(data)

row_sums <- rowSums(data[, c("Val2", "val4")], na.rm = TRUE)
print("Row Sums:")
print(row_sums)

Output:

rowsum5

Rowsum for selected columns

In this article, we explored how to compute row sums in R and also demonstrated how to calculate the sums for specific columns using the rowSums() function, with options to exclude NA values.




Next Article
Article Tags :

Similar Reads