In R, you can calculate row and column sums of matrices using the rowSums()
and colSums()
functions, respectively.
In this example,
mat
.rowSums()
function to calculate the sum of elements in each row of the matrix mat
.row_sums
.row_sums
to the console to display the row sums of the matrix.mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
row_sums <- rowSums(mat)
print(row_sums)
[1] 6 15
In this example,
mat
.colSums()
function to calculate the sum of elements in each column of the matrix mat
.col_sums
.col_sums
to the console to display the column sums of the matrix.mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
col_sums <- colSums(mat)
print(col_sums)
[1] 5 7 9
In this tutorial, we learned How to Calculate Row and Column Sums in R language with well detailed examples.