R语言 base包-apply函数_R语言培训
pply {base} R Documentation R文档
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.
对于给定向量的描述,以提供给给定函数进行计算。例如:1表示行,2表示列,c(1,2)表示行和列。x表示维数,如果向量具有维名,可以用自符向量代替。
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.
如果X不是一个矩阵,而是一个无维值得类对象,如:数据框,如果它是二维的话,apply()则尝试通过as.matrix()强制转换为阵列。
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.
FUN是一个可以通过调用match.fun()函数查看的函数、符号(反引号)或者能够通过调用apply()从环境变量中搜索到的字符函数。
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.
如果每次调用函数返回一个长度为n的向量,那么apply()将会返回一个维数为c(n, dim(X)[MARGIN])的阵列。如果n>1,apply()将会返回一个向量。如果n为0,结果长度为0,但是没有必要修正维数。
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.
如果调用函数返回的是不同长度的向量,则返回一个维数为MARGIN(MARIGIN长度页大于1)长度的列表,列表长度为prod(dim(X)[MARGIN])。
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.
在维数设定前,结果会由as.vector()强制转换为一个基本向量类型。例如:因子结果会被转换为一个字符阵列。
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)) #按照列组合3和c(4:1,2:5)列表
dimnames(x)[[1]] <- letters[1:8] #将x的行维名字修改为a-h
apply(x, 2, mean, trim = .2) #用apply()调用,对列求均值,求均值前除去最大最小的20%部分
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
转载请注明:数据分析 » R语言 base包-apply函数_R语言培训