Compute the Sum of Rows of a Matrix or Array in R Programming - rowSums Function
Last Updated :
30 Apr, 2025
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:
Row sum across 1-D arrayExample 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:
Row sum in a 3-D arrayExample 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:
Row sum in a dataframeExample 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:
Row sum in a datarfame with missing valuesExample 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:
Rowsum for selected columnsIn 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.
Similar Reads
Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function colSums() function in R Language is used to compute the sums of matrix or array columns. Syntax: colSums (x, na.rm = FALSE, dims = 1) Parameters: x: matrix or array dims: this is integer value whose dimensions are regarded as âcolumnsâ to sum over. It is over dimensions 1:dims. Example 1: Python3 #
2 min read
Calculate the Mean of each Row of an Object in R Programming â rowMeans() Function rowMeans() function in R Language is used to find out the mean of each row of a data frame, matrix, or array. Syntax: rowMeans(data) Parameter: data: data frame, array, or matrix Example 1 Python3 1== # R program to illustrate # rowMean function # Create example values # Set seed set.seed(1234) # Cr
1 min read
Calculate Trace of a Matrix in R Programming - tr() Function tr() function in R Language is used to calculate the trace of a matrix. Trace of a matrix is the sum of the values on the main diagonal(upper left to lower right) of the matrix. Syntax: tr(x) Parameters: x: Matrix Example 1: Python3 1== # R program to calculate # trace of a matrix # Loading library
1 min read
Calculate the Mean of each Column of a Matrix or Array in R Programming - colMeans() Function colMeans() function in R Language is used to compute the mean of each column of a matrix or array. Syntax: colMeans(x, dims = 1) Parameters: x: array of two or more dimensions, containing numeric, complex, integer or logical values, or a numeric data frame dims: integer value, which dimensions are r
2 min read
Getting a Matrix of number of rows in R Programming - row() Function In this article, we will discuss how to find the number of rows and columns in a matrix in R Programming Language. nrow() Function in Rnrow() function in R Language is used to get the row number of a matrix. Syntax: row(x, as.factor=FALSE) Parameters:x: matrixas.factor: a logical value indicating w
2 min read