矩阵行列, 和, 数学期望计算 - rowSums,rowMeans,colSums,colMeans

简介:
rowSums
rowMeans
colSums
colMeans
在R中很容易计算一个矩阵的各行和和各列和以及各行的平均值和各列的平均值。例如:

> A=matrix(1:12,3,4)

> A
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

> rowSums(A)
[1] 22 26 30

> rowMeans(A)
[1] 5.5 6.5 7.5

> colSums(A)
[1]  6 15 24 33

> colMeans(A)
[1]  2  5  8 11

除此之外, 使用apply函数, 也可以计算. 而且更灵活.
> args(apply)
function (X, MARGIN, FUN, ...)
其中:x为矩阵,MARGIN用来指定是对行运算还是对列运算,MARGIN=1表示对行运算,MARGIN=2表示对列运算,FUN用来指定运算函数, ...用来给定FUN中需要的其它的参数,例如:
计算每行的sum.
> apply(A,1,sum)
[1] 22 26 30
计算每行的数学期望.
> apply(A,1,mean)
[1] 5.5 6.5 7.5
计算每列的sum.
> apply(A,2,sum)
[1] 6 15 24 33
计算每列的数学期望.
> apply(A,2,mean)
[1] 2 5 8 11

apply()函数功能强大,我们可以对矩阵的行或者列进行其它运算,例如:
计算每一列的方差(var函数求方差)
> A=matrix(rnorm(100),20,5)
> apply(A,2,var)
[1] 0.4641787 1.4331070 0.3186012 1.3042711 0.5238485
还可以使用类似lambda风格.
> apply(A,2, function(x,a) x*a,a=2) 
  [,1] [,2] [,3] [,4]
[1,]   2   8   14   20
[2,]   4   10   16   22
[3,]   6   12   18   24
注意:apply(A,2,function(x,a)x*a,a=2)与A*2效果相同,此处旨在说明如何应用alpply函数。

[参考]
1. > help("apply")
apply                   package:base                   R Documentation

Apply Functions Over Array Margins

Description:

     Returns a vector or array or list of values obtained by applying a
     function to margins of an array or matrix.

Usage:

     apply(X, MARGIN, FUN, ...)
     
Arguments:

       X: an array, including a matrix.

  MARGIN: a vector giving the subscripts which the function will be
          applied over.  E.g., for a matrix ‘1’ indicates rows, ‘2’
          indicates columns, ‘c(1, 2)’ indicates rows and columns.
          Where ‘X’ has named dimnames, it can be a character vector
          selecting dimension names.

     FUN: the function to be applied: see ‘Details’.  In the case of
          functions like ‘+’, ‘%*%’, etc., the function name must be
          backquoted or quoted.

     ...: optional arguments to ‘FUN’.

Details:

     If ‘X’ is not an array but an object of a class with a non-null
     ‘dim’ value (such as a data frame), ‘apply’ attempts to coerce it
     to an array via ‘as.matrix’ if it is two-dimensional (e.g., a data
     frame) or via ‘as.array’.

     ‘FUN’ is found by a call to ‘match.fun’ and typically is either a
     function or a symbol (e.g., a backquoted name) or a character
     string specifying a function to be searched for from the
     environment of the call to ‘apply’.

     Arguments in ‘...’ cannot have the same name as any of the other
     arguments, and care may be needed to avoid partial matching to
     ‘MARGIN’ or ‘FUN’.  In general-purpose code it is good practice to
     name the first three arguments if ‘...’ is passed through: this
     both avoids partial matching to ‘MARGIN’ or ‘FUN’ and ensures that
     a sensible error message is given if arguments named ‘X’, ‘MARGIN’
     or ‘FUN’ are passed through ‘...’.

Value:

     If each call to ‘FUN’ returns a vector of length ‘n’, then ‘apply’
     returns an array of dimension ‘c(n, dim(X)[MARGIN])’ if ‘n > 1’.
     If ‘n’ equals ‘1’, ‘apply’ returns a vector if ‘MARGIN’ has length
     1 and an array of dimension ‘dim(X)[MARGIN]’ otherwise.  If ‘n’ is
     ‘0’, the result has length 0 but not necessarily the ‘correct’
     dimension.

     If the calls to ‘FUN’ return vectors of different lengths, ‘apply’
     returns a list of length ‘prod(dim(X)[MARGIN])’ with ‘dim’ set to
     ‘MARGIN’ if this has length greater than one.

     In all cases the result is coerced by ‘as.vector’ to one of the
     basic vector types before the dimensions are set, so that (for
     example) factor results will be coerced to a character array.

References:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_.  Wadsworth & Brooks/Cole.

See Also:

     ‘lapply’ and there, ‘simplify2array’; ‘tapply’, and convenience
     functions ‘sweep’ and ‘aggregate’.

Examples:

     ## Compute row and column sums for a matrix:
     x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
     dimnames(x)[[1]] <- letters[1:8]
     apply(x, 2, mean, trim = .2)
     col.sums <- apply(x, 2, sum)
     row.sums <- apply(x, 1, sum)
     rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums, sum(col.sums)))
     
     stopifnot( apply(x, 2, is.vector))
     
     ## Sort the columns of a matrix
     apply(x, 2, sort)
     
     ##- function with extra args:
     cave <- function(x, c1, c2) c(mean(x[c1]), mean(x[c2]))
     apply(x, 1, cave,  c1 = "x1", c2 = c("x1","x2"))
     
     ma <- matrix(c(1:4, 1, 6:8), nrow = 2)
     ma
     apply(ma, 1, table)  #--> a list of length 2
     apply(ma, 1, stats::quantile) # 5 x n matrix with rownames
     
     stopifnot(dim(ma) == dim(apply(ma, 1:2, sum)))
     
     ## Example with different lengths for each call
     z <- array(1:24, dim = 2:4)
     zseq <- apply(z, 1:2, function(x) seq_len(max(x)))
     zseq         ## a 2 x 3 matrix
     typeof(zseq) ## list
     dim(zseq) ## 2 3
     zseq[1,]
     apply(z, 3, function(x) seq_len(max(x)))
     # a list without a dim attribute

目录
相关文章
|
Linux
linux grep查看指定内容上下几行
linux系统中,可以利用grep查看指定的内容, 比如:grep “123” test.log //查看test.
5067 0
|
缓存 JavaScript Java
常见java OOM异常分析排查思路分析
Java虚拟机(JVM)遇到内存不足时会抛出OutOfMemoryError(OOM)异常。常见OOM情况包括:1) **Java堆空间不足**:大量对象未被及时回收或内存泄漏;2) **线程栈空间不足**:递归过深或大量线程创建;3) **方法区溢出**:类信息过多,如CGLib代理类生成过多;4) **本机内存不足**:JNI调用消耗大量内存;5) **GC造成的内存不足**:频繁GC但效果不佳。解决方法包括调整JVM参数(如-Xmx、-Xss)、优化代码及使用高效垃圾回收器。
590 15
常见java OOM异常分析排查思路分析
|
安全 异构计算
为大型语言模型 (LLM) 提供服务需要多少 GPU 内存?
为大型语言模型 (LLM) 提供服务需要多少 GPU 内存?
为大型语言模型 (LLM) 提供服务需要多少 GPU 内存?
|
Web App开发 Linux 微服务
了解应用中的微内核架构
【6月更文挑战第25天】**微内核架构**是将系统服务从内核移出,形成可选插件,增强扩展性和适应性。常见于第三方应用和嵌入式系统,如Linux、L4、WinCE。优点包括清晰结构、移植性和扩展性,但缺点是通信开销大、性能较低,不利于整体优化。适合需要灵活功能组合的场景。
442 5
了解应用中的微内核架构
|
运维 C# UED
C# 一分钟浅谈:异常处理的最佳实践
【9月更文挑战第5天】在软件开发中,异常处理对保证程序稳定性和用户体验至关重要。本文从基础概念入手,详细讲解C#中的异常处理策略,并通过代码示例说明如何有效实现异常管理。文章涵盖`try`、`catch`和`finally`块的使用,探讨常见问题如忽略异常和过度捕获,并提出最佳实践建议,如使用具体异常类型、记录异常信息及优雅地处理异常,助力开发者构建更健壮的应用程序。
636 1
|
安全 数据挖掘 网络安全
电子商务网站建设的关键技术
移动端适配是电子商务网站建设中越来越重要的关键技术。综上所述,电子商务网站建设涉及多个关键技术,包括网站设计与用户体验、安全性、性能与可扩展性、搜索引擎优化、移动端适配以及数据分析与营销。掌握这些关键技术,企业可以建设一个安全、高效、用户…
711 0
|
存储 关系型数据库 数据挖掘
第11章‘数据库设计规范(1)
第11章‘数据库设计规范
214 0
|
存储 SQL 关系型数据库
MySQL 5.7和 MySQL8.0 InnoDB auto_increment 初始化的区别
在MySQL 5.7及之前,自动递增计数器只存于内存,重启后需通过查询确定初始值。从MySQL 8.0开始,计数器变化时写入重做日志,检查点时保存至数据字典,确保重启后能基于持久化的最大值初始化,避免查询,增强连续性和一致性。[[MySQL参考手册, 3099页]](https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html)
|
移动开发 算法 Java
经验大分享:PAML简介
经验大分享:PAML简介
546 0