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 regarded as ‘columns’ to sum over. It is over dimensions 1:dims.
Example 1:
# R program to illustrate # colMeans function # Initializing a matrix with # 3 rows and 3 columns x <- matrix(rep(1:9), 3, 3) # Getting matrix representation x # Calling the colMeans() function colMeans(x) |
Output:
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
[1] 2 5 8
Example 2:
# R program to illustrate # colMeans function # Initializing a 3D array x <- array(1:12, c(2, 3, 3)) # Getting the array representation x # Calling the colMeans() function # for dims = 1, x[, 1, 1], x[, 2, 1], x[, 3, 1], # x[, 1, 2] ... are columns colMeans(x, dims = 1) # for dims = 2, x[,,1], x[,,2], x[,,3] # are columns colMeans(x, dims = 2) |
Output:
,, 1
[, 1] [, 2] [, 3]
[1, ] 1 3 5
[2, ] 2 4 6,, 2
[, 1] [, 2] [, 3]
[1, ] 7 9 11
[2, ] 8 10 12,, 3
[, 1] [, 2] [, 3]
[1, ] 1 3 5
[2, ] 2 4 6
[, 1] [, 2] [, 3]
[1, ] 1.5 7.5 1.5
[2, ] 3.5 9.5 3.5
[3, ] 5.5 11.5 5.5
[1] 3.5 9.5 3.5
Recommended Posts:
- Calculate Arithmetic mean in R Programming - mean() Function
- Calculate the Weighted Mean in R Programming - weighted.mean() Function
- Calculate the Mean of each Row of an Object in R Programming – rowMeans() Function
- Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function
- Calculate the cross-product of a Matrix in R Programming - crossprod() Function
- Calculate the cross-product of the Transpose of a Matrix in R Programming - tcrossprod() Function
- Calculate Trace of a Matrix in R Programming - tr() Function
- Get the position of the maximum element in each Row of a Matrix in R Programming - max.col() Function
- Mean, Median and Mode in R Programming
- Root-Mean-Square Error in R Programming
- Calculate arc cosine of a value in R programming - acos() function
- Calculate arc tangent of a value in R programming - atan2(y, x) function
- Calculate cosine of a value in R Programming - cos() Function
- Calculate Hyperbolic cosine of a value in R Programming - cosh() Function
- Calculate sine of a value in R Programming - sin() Function
- Calculate Hyperbolic sine of a value in R Programming - sinh() Function
- Calculate Hyperbolic tangent of a value in R Programming - tanh() Function
- Calculate tangent of a value in R Programming - tan() Function
- Calculate Inverse sine of a value in R Programming - asin() Function
- Calculate Inverse cosine of a value in R Programming - acos() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

