R 语言中的 colMeans() 函数用于计算矩阵或数组的每一列的均值。
用法: colMeans(x, dims = 1)
参数:
x:二维或更多维度的数组,包含数值、复数、整数或逻辑值,或数值 DataFrame
dims:整数值,将哪些维度视为 ‘columns’ 求和。它超过尺寸 1:dims。
范例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)
输出:
[, 1] [, 2] [, 3] [1, ] 1 4 7 [2, ] 2 5 8 [3, ] 3 6 9 [1] 2 5 8
范例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)
输出:
,, 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
相关用法
- R语言 mean()用法及代码示例
- R语言 weighted.mean()用法及代码示例
- R语言 rowMeans()用法及代码示例
- R语言 colSums()用法及代码示例
- R语言 tr()用法及代码示例
- R语言 max.col()用法及代码示例
- R语言 is.matrix()用法及代码示例
- R语言 data.matrix()用法及代码示例
- R语言 as.matrix()用法及代码示例
- R语言 acos()用法及代码示例
- R语言 cos()用法及代码示例
- R语言 cosh()用法及代码示例
- R语言 sin()用法及代码示例
- R语言 sinh()用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Calculate the Mean of each Column of a Matrix or Array in R Programming – colMeans() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。