In R, you can calculate row and column means of matrices using the rowMeans()
and colMeans()
functions, respectively.
In this example,
mat
.rowMeans()
function to calculate the mean of elements in each row of the matrix mat
.row_means
.row_means
to the console to display the row means of the matrix.mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
row_means <- rowMeans(mat)
print(row_means)
[1] 2.5 3.5
In this example,
mat
.colMeans()
function to calculate the mean of elements in each column of the matrix mat
.col_means
.col_means
to the console to display the column means of the matrix.mat <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
col_means <- colMeans(mat)
print(col_means)
[1] 3 4 5
In this tutorial, we learned How to Calculate Row and Column Means in R language with well detailed examples.