总结:
就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传递给FUN)。apply的返回值结果可能是向量,数组(含矩阵)或列表(具体要根据条件分类讨论,但实际上我们没有必要,直接看一下返回的结果就知道是什么类型了)
apply {base}
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 数组,包括矩阵
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(backquote是反引用的意思) 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不是数组,但是一个有非空的dim属性值的类对象(如数据框),apply函数将试图使用as.matrix()(当X是二维的时候)或使用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.
注:match.fun(FUN, descend = TRUE)(Value(返回值):A function matching FUN or an error is generated.)也就是返回一个function类型的与FUN匹配的函数
FUN通常是通过调用math.fun的函数来找到的,通常是一个函数或一个符号(如一个反引用变量)或者一个字符串指定一个函数(从环境中搜索到然后调用)
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 ....
可变参数...中的参数不能和任何其他的参数名一样,并且要注意要避免与MARGIN或FUN部分匹配(就是名字不要和MARGIN或FUN形似咯?)给前三个传递过去的变量命名是个好习惯。
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.
每次调用FUN会返回一个长度为n的向量
n>1,则apply返回一个维度为c(n,dim(X)[MARGIN])的数组
n=1且MARGIN的长度为1(说明传入的是一个二维的矩阵),则apply返回一个向量,如果n=1,而MARGIN的长度不为1,则返回一个维度为dim(X)[MARGIN]的数组
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.
如果每次FUN返回的向量有不同的长度,则apply将返回一个长度为prod(dim(X)[MARGIN])的列表,如果其长度大于1,则将dim设置给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函数转换,所以因子的结果通常被转换为字符数组。
例:
test<-matrix(1:20,ncol=4)
#既然给定了列数,会自动计算行数
apply(test,c(1,2),mean)
# [,1] [,2] [,3] [,4]
# [1,] 1 6 11 16
# [2,] 2 7 12 17
# [3,] 3 8 13 18
# [4,] 4 9 14 19
# [5,] 5 10 15 20
apply(test,1,mean)
# [1] 8.5 9.5 10.5 11.5 12.5
# 返回的是一个向量
apply函数的源码(直接F2即可)
为了方便理解源码,我们使用一个特例
x<-matrix(1:6,2)
源码:
function (X, MARGIN, FUN, ...)
{
FUN <- match.fun(FUN) #找到匹配的函数
dl <- length(dim(X)) #取到X中是几维 dl=2
if (!dl)
stop("dim(X) must have a positive length")
if (is.object(X)) #盘判断是否class属性
X <-
if (dl == 2L) #维度为2,则转化为矩阵as.matrix(X)
else
as.array(X) #否则转发转化为数组
d <- dim(X) #d是一个向量,里面存放着X的每一个维度 d=[1] 2 3
dn <- dimnames(X) #如果没有指定维度名,则dn=NULL,一般都是NULL
ds <- seq_len(dl) # 产生一个1到dl的向量 ds=[1] 1 2
if (is.character(MARGIN)) { #
MARGIN是否为字符(我们没指定维度名,这个不考虑)if (is.null(dnn <- names(dn)))
stop("'X' must have named dimnames")
MARGIN <- match(MARGIN, dnn)
if (anyNA(MARGIN))
stop("not all elements of 'MARGIN' are names of dimensions")
}
s.call <- ds[-MARGIN] #MARGIN是1或2,假设MARGIN=1 s.call=2
s.ans <- ds[MARGIN] #s.ans=1
d.call <- d[-MARGIN] #d.call=3
d.ans <- d[MARGIN] #第MARGIN个维度的位数 d.ans=2
dn.call <- dn[-MARGIN] #NULL 不考虑
dn.ans <- dn[MARGIN]
#NULL 不考虑d2 <- prod(d.ans) #连乘 d2=2
if (d2 == 0L) { #我们的一般情况不会出现该维度为0
newX <- array(vector(typeof(X), 1L), dim = c(prod(d.call),
1L))
ans <- forceAndCall(1, FUN, if (length(d.call) < 2L) newX[,
1] else array(newX[, 1L], d.call, dn.call), ...)
return(if (is.null(ans)) ans else if (length(d.ans) <
2L) ans[1L][-1L] else array(ans, d.ans, dn.ans))
}
newX <- aperm(X, c(s.call, s.ans)) #c(2,1)
#理解aperm函数就知道,当X是一个矩阵的时候,其实这等价于一个转置
[,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6
dim(newX) <- c(prod(d.call), d2) # 3,2
ans <- vector("list", d2) #创建一个包含两个组件的列表
[[1]] NULL [[2]] NULL
if (length(d.call) < 2L) { #d.call=3,不成立
if (length(dn.call))
dimnames(newX) <- c(dn.call, list(NULL))
for (i in 1L:d2) {
tmp <- forceAndCall(1, FUN, newX[, i], ...)
if (!is.null(tmp))
ans[[i]] <- tmp
}
}
else for (i in 1L:d2) { #d2=2 #执行
tmp <- forceAndCall(1, FUN, array(newX[, i], d.call,
dn.call), ...)- #传给apply的要被处理的数据是在这里才被传递给FUN的
if (!is.null(tmp)) #判断是否为空
ans[[i]] <- tmp
}
#此时ans
[[1]] [1] 3 #newX第一列的均值 [[2]] [1] 4
ans.list <- is.recursive(ans[[1L]])
#[1] FALSE
l.ans <- length(ans[[1L]]) # l.ans=1
ans.names <- names(ans[[1L]]) #ans.names=NULL
if (!ans.list) #成立
ans.list <- any(lengths(ans) != l.ans)
#lengths(ans) [1] 1 1 即每个组件中的元素的个数
#[1] FALSE FALSE ----> ans.list = FALSE
if (!ans.list && length(ans.names)) { #length(ans.names)=0 所以整个是F,不成立
all.same <- vapply(ans, function(x) identical(names(x),
ans.names), NA)
if (!all(all.same))
ans.names <- NULL
}
len.a <- if (ans.list) #不成立
d2
else length(ans <- unlist(ans, recursive = FALSE)) # len.a=2
if (length(MARGIN) == 1L && len.a == d2) { #满足
names(ans) <- if (length(dn.ans[[1L]])) #dn.ans是null
dn.ans[[1L]] #不会执行
ans # [1] 3 4 最终整个作为返回值
}
else if (len.a == d2)
array(ans, d.ans, dn.ans)
else if (len.a && len.a%%d2 == 0L) {
if (is.null(dn.ans))
dn.ans <- vector(mode = "list", length(d.ans))
dn1 <- list(ans.names)
if (length(dn.call) && !is.null(n1 <- names(dn <- dn.call[1])) &&
nzchar(n1) && length(ans.names) == length(dn[[1]]))
names(dn1) <- n1
dn.ans <- c(dn1, dn.ans)
array(ans, c(len.a%/%d2, d.ans), if (!is.null(names(dn.ans)) ||
!all(vapply(dn.ans, is.null, NA)))
dn.ans)
}
else ans
}
补充1
seq_len()函数是seq函数族中的一个,用法如下:
seq_len(length.out)
length.out
desired length of the sequence. A non-negative number, which for seq and seq.int will be rounded up if fractional.(想要生成的序列的个数)
其实就是seq_len(n) 产生n个数(从1到n)
补充2
aperm方法的用法详见笔记
补充3
补充4
补充5
is.recursive returns TRUE if x has a recursive (list-like) structure(递归结构) and FALSE otherwise.
###################################################
关于...参数是如何传递给FUN的
事实上,上面的分析结合下面金的分析,我得出的结论是:
forceAndCall将X和...传递给了FUN,事实上,我们可以将...看做一个一组不确定个数和名字的变量(或者把他当做一个记号吧~)
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
dimnames(x)[[1]] <- letters[1:8]
x
x1 x2
a 3 4
b 3 3
c 3 2
d 3 1
e 3 2
f 3 3
g 3 4
h 3 5
apply(x, 2, mean, trim = .2)
x1 x2
3 3
function (X, MARGIN, FUN, ...)
{
FUN <- match.fun(FUN)
dl <- length(dim(X)) #dl=2
if (!dl)
stop("dim(X) must have a positive length")
if (is.object(X))
X <- if (dl == 2L)
as.matrix(X) #例子中x本就是matrix
else as.array(X)
d <- dim(X) #d=[1] 8 2
dn <- dimnames(X)
# [[1]]
# [1] "a" "b" "c" "d" "e" "f" "g" "h"
#
# [[2]]
# [1] "x1" "x2"
ds <- seq_len(dl) #ds=1 2
if (is.character(MARGIN)) { #MARGIN=2,不是字符
if (is.null(dnn <- names(dn)))
stop("'X' must have named dimnames")
MARGIN <- match(MARGIN, dnn)
if (anyNA(MARGIN))
stop("not all elements of 'MARGIN' are names of dimensions")
}
s.call <- ds[-MARGIN] #s.call=1
s.ans <- ds[MARGIN] #s.ans=2
d.call <- d[-MARGIN] #d.call=8
d.ans <- d[MARGIN] #d.ans=2
dn.call <- dn[-MARGIN]
# [[1]]
# [1] "a" "b" "c" "d" "e" "f" "g" "h"
dn.ans <- dn[MARGIN]
# [[1]]
# [1] "x1" "x2"
d2 <- prod(d.ans) #d2=2
if (d2 == 0L) { #跳过
newX <- array(vector(typeof(X), 1L), dim = c(prod(d.call),
1L))
ans <- forceAndCall(1, FUN, if (length(d.call) < 2L) newX[,
1] else array(newX[, 1L], d.call, dn.call), ...)
return(if (is.null(ans)) ans else if (length(d.ans) <
2L) ans[1L][-1L] else array(ans, d.ans, dn.ans))
}
newX <- aperm(X, c(s.call, s.ans)) #perm=c(1,2),所以相当于没变
# x1 x2
# a 3 4
# b 3 3
# c 3 2
# d 3 1
# e 3 2
# f 3 3
# g 3 4
# h 3 5
dim(newX) <- c(prod(d.call), d2) #8,2
# [,1] [,2]
# [1,] 3 4
# [2,] 3 3
# [3,] 3 2
# [4,] 3 1
# [5,] 3 2
# [6,] 3 3
# [7,] 3 4
# [8,] 3 5
#重定义了下维度就没有dimnames属性啦?
ans <- vector("list", d2)
# [[1]]
# NULL
#
# [[2]]
# NULL
if (length(d.call) < 2L) { #d.call=8
if (length(dn.call))
dimnames(newX) <- c(dn.call, list(NULL))
for (i in 1L:d2) {
tmp <- forceAndCall(1, FUN, newX[, i], ...)
if (!is.null(tmp))
ans[[i]] <- tmp
}
}
else for (i in 1L:d2) { #执行
tmp <- forceAndCall(1, FUN, array(newX[, i], d.call,
dn.call), ...)
#我经过反复的测试,得到trim = .2这个参数其实是传递给了...
#只不过这里不巧的是mean(newX)和mean(newX,.2)的结果都是3
if (!is.null(tmp))
ans[[i]] <- tmp
}
ans.list <- is.recursive(ans[[1L]])
l.ans <- length(ans[[1L]])
ans.names <- names(ans[[1L]])
if (!ans.list)
ans.list <- any(lengths(ans) != l.ans)
if (!ans.list && length(ans.names)) {
all.same <- vapply(ans, function(x) identical(names(x),
ans.names), NA)
if (!all(all.same))
ans.names <- NULL
}
len.a <- if (ans.list)
d2
else length(ans <- unlist(ans, recursive = FALSE))
if (length(MARGIN) == 1L && len.a == d2) {
names(ans) <- if (length(dn.ans[[1L]]))
dn.ans[[1L]]
ans
}
else if (len.a == d2)
array(ans, d.ans, dn.ans)
else if (len.a && len.a%%d2 == 0L) {
if (is.null(dn.ans))
dn.ans <- vector(mode = "list", length(d.ans))
dn1 <- list(ans.names)
if (length(dn.call) && !is.null(n1 <- names(dn <- dn.call[1])) &&
nzchar(n1) && length(ans.names) == length(dn[[1]]))
names(dn1) <- n1
dn.ans <- c(dn1, dn.ans)
array(ans, c(len.a%/%d2, d.ans), if (!is.null(names(dn.ans)) ||
!all(vapply(dn.ans, is.null, NA)))
dn.ans)
}
else ans
}
删掉了那些执行不到的代码和一些与我们的问题无关的代码
function (X, MARGIN, FUN, ...)
{
FUN <- match.fun(FUN)
dl <- length(dim(X)) #dl=2
if (!dl)
stop("dim(X) must have a positive length")
if (is.object(X))
X <- if (dl == 2L)
as.matrix(X) #例子中x本就是matrix
else as.array(X)
d <- dim(X) #d=[1] 8 2
dn <- dimnames(X)
# [[1]]
# [1] "a" "b" "c" "d" "e" "f" "g" "h"
#
# [[2]]
# [1] "x1" "x2"
ds <- seq_len(dl) #ds=1 2
s.call <- ds[-MARGIN] #s.call=1
s.ans <- ds[MARGIN] #s.ans=2
d.call <- d[-MARGIN] #d.call=8
d.ans <- d[MARGIN] #d.ans=2
dn.call <- dn[-MARGIN]
# [[1]]
# [1] "a" "b" "c" "d" "e" "f" "g" "h"
dn.ans <- dn[MARGIN]
# [[1]]
# [1] "x1" "x2"
d2 <- prod(d.ans) #d2=2
newX <- aperm(X, c(s.call, s.ans)) #perm=c(1,2),所以相当于没变
# x1 x2
# a 3 4
# b 3 3
# c 3 2
# d 3 1
# e 3 2
# f 3 3
# g 3 4
# h 3 5
dim(newX) <- c(prod(d.call), d2) #8,2
# [,1] [,2]
# [1,] 3 4
# [2,] 3 3
# [3,] 3 2
# [4,] 3 1
# [5,] 3 2
# [6,] 3 3
# [7,] 3 4
# [8,] 3 5
#重定义了下维度就没有dimnames属性啦?
ans <- vector("list", d2)
# [[1]]
# NULL
#
# [[2]]
# NULL
if (length(d.call) < 2L) { #d.call=8
if (length(dn.call))
dimnames(newX) <- c(dn.call, list(NULL))
for (i in 1L:d2) {
tmp <- forceAndCall(1, FUN, newX[, i], ...)
if (!is.null(tmp))
ans[[i]] <- tmp
}
}
else for (i in 1L:d2) { #执行
tmp <- forceAndCall(1, FUN, array(newX[, i], d.call,
dn.call), ...)
#我经过反复的测试,得到trim = .2这个参数其实是传递给了...
#只不过这里不巧的是mean(newX)和mean(newX,.2)的结果都是3
}
###############################################
余下的例子:
## 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)
# x1 x2
# 24 24
#求每一行的和
row.sums <- apply(x, 1, sum)
# a b c d e f g h
# 7 6 5 4 5 6 7 8
rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums, sum(col.sums)))
# x1 x2 Rtot
# a 3 4 7
# b 3 3 6
# c 3 2 5
# d 3 1 4
# e 3 2 5
# f 3 3 6
# g 3 4 7
# h 3 5 8
# Ctot 24 24 48
> apply(x, 2, is.vector)
x1 x2
TRUE TRUE
## Sort the columns of a matrix
- #按列排序,排序完了列名就木有啦?
apply(x, 2, sort)
# x1 x2
# [1,] 3 1
# [2,] 3 2
# [3,] 3 2
# [4,] 3 3
# [5,] 3 3
# [6,] 3 4
# [7,] 3 4
# [8,] 3 5
为了自己探索上面的行名在排序后消失的困惑,我自己写了如下的代码测试
> a<-c(2,11,7,13)
> b<-c(3,5,9,2)
> m<-cbind(a=a,b=b)
> dimnames(m)<-list(paste(LETTERS[1:4],1:4,sep = "-"),c(letters[1:2]))
> m
a b
A-1 2 3
B-2 11 5
C-3 7 9
D-4 13 2
> apply(m,2,sort)
a b
[1,] 2 2
[2,] 7 3
[3,] 11 5
[4,] 13 9
> apply(m,1,sort)
A-1 B-2 C-3 D-4
[1,] 2 5 7 2
[2,] 3 11 9 13
所以,你会发现,这种排序可能对于矩阵还有点用,对于数据框完全没有意义,因为他把行给拆开了,本来2对应3,现在变成了2对应2,所以说,他的行名自然也就消失了,因为这个行已经不是原来的那个行,自然也不能用原来的行名了(容易误导),同样的,按行排序也是。
注意, LETTERS是一个常量而不是函数
查看帮助文档会发现LETTERS是在Constants下的,Constants中存放的是R中的常量。只有四个(LETTERS,letters,month.abb,month.name,pi)
LETTERS: the 26 upper-case letters of the Roman alphabet(拉丁字母/罗马字母);
26个大写英文字母
letters: the 26 lower-case letters of the Roman alphabet;
26个小写的英文字母
month.abb: the three-letter abbreviations for the English month names;
三个字母缩写的12月份名
month.name: the English names for the months of the year;
12月份名(全名)
pi: the ratio of the circumference of a circle to its diameter.
例子2:
其实我不太清楚例子2想说明什么,想说明维度名会保存么?不过我们这还少学会了如何设置维度名
- x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
x
# x1 x2
# [1,] 3 4
# [2,] 3 3
# [3,] 3 2
# [4,] 3 1
# [5,] 3 2
# [6,] 3 3
# [7,] 3 4
# [8,] 3 5
## keeping named dimnames
#给维度命名
names(dimnames(x)) <- c("row", "col")
#给维度命名
x
# col
# row x1 x2
# [1,] 3 4
# [2,] 3 3
# [3,] 3 2
# [4,] 3 1
# [5,] 3 2
# [6,] 3 3
# [7,] 3 4
# [8,] 3 5
x3 <- array(x, dim = c(dim(x),3),
dimnames = c(dimnames(x),
list(C = paste0("cop.",1:3))))
x3
# , , C = cop.1
#
# col
# row x1 x2
# [1,] 3 4
# [2,] 3 3
# [3,] 3 2
# [4,] 3 1
# [5,] 3 2
# [6,] 3 3
# [7,] 3 4
# [8,] 3 5
#
# , , C = cop.2
#
# col
# row x1 x2
# [1,] 3 4
# [2,] 3 3
# [3,] 3 2
# [4,] 3 1
# [5,] 3 2
# [6,] 3 3
# [7,] 3 4
# [8,] 3 5
#
# , , C = cop.3
#
# col
# row x1 x2
# [1,] 3 4
# [2,] 3 3
# [3,] 3 2
# [4,] 3 1
# [5,] 3 2
# [6,] 3 3
# [7,] 3 4
# [8,] 3 5
identical(x, apply( x, 2, identity))
# [1] TRUE
identical(x3, apply(x3, 2:3, identity))
# [1] TRUE
> apply( x, 2, identity)
col
row x1 x2
[1,] 3 4
[2,] 3 3
[3,] 3 2
[4,] 3 1
[5,] 3 2
[6,] 3 3
[7,] 3 4
[8,] 3 5
> apply(x3, 2:3, identity) #对数组的列和层引用identity函数
, , C = cop.1
col
row x1 x2
[1,] 3 4
[2,] 3 3
[3,] 3 2
[4,] 3 1
[5,] 3 2
[6,] 3 3
[7,] 3 4
[8,] 3 5
, , C = cop.2
col
row x1 x2
[1,] 3 4
[2,] 3 3
[3,] 3 2
[4,] 3 1
[5,] 3 2
[6,] 3 3
[7,] 3 4
[8,] 3 5 ###下面这段输出结果第一次忘了插入了
, , C = cop.3
col
row x1 x2
[1,] 3 4
[2,] 3 3
[3,] 3 2
[4,] 3 1
[5,] 3 2
[6,] 3 3
[7,] 3 4
[8,] 3 5
这里我比较奇怪的是,为什么输出的都是TRUE(先用了identity再用identical函数判断,当然是TRUE了)
查看了identity的说明:
A trivial(琐碎的微小的) identity(识别) function returning its argument.
这什么鬼?还不带例子
identical的描述是:
The safe and reliable way to test two objects for being exactly equal. It returns TRUE in this case, FALSE in every other case.
安全而可靠的测试两个变量是否完全相同。
例子3:
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
> x
x1 x2
[1,] 3 4
[2,] 3 3
[3,] 3 2
[4,] 3 1
[5,] 3 2
[6,] 3 3
[7,] 3 4
[8,] 3 5
cave <- function(x, c1, c2) {
c(mean(x[c1]), mean(x[c2]))
}
apply(x, 1, cave, c1 = "x1", c2 = c("x1","x2"))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3.0 3 3.0 3 3.0 3 3.0 3
[2,] 3.5 3 2.5 2 2.5 3 3.5 4
> class(apply(x, 1, cave, c1 = "x1", c2 = c("x1","x2")))
[1] "matrix"
我通过下面的例子,来说明MARGIN=1和x以及c1和c2的传递方式
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
##- function with extra args:
cave <- function(x, c1, c2) {
print("##q##")
print(x)
print("==b==")
c(mean(x[c1]), mean(x[c2]))
}
apply(x, 1, cave, c1 = "x1", c2 = c("x1","x2"))
[1] "##q##"
x1 x2
3 4
[1] "==b=="
[1] "##q##"
x1 x2
3 3
[1] "==b=="
[1] "##q##"
x1 x2
3 2
[1] "==b=="
[1] "##q##"
x1 x2
3 1
[1] "==b=="
[1] "##q##"
x1 x2
3 2
[1] "==b=="
[1] "##q##"
x1 x2
3 3
[1] "==b=="
[1] "##q##"
x1 x2
3 4
[1] "==b=="
[1] "##q##"
x1 x2
3 5
[1] "==b=="
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 3.0 3 3.0 3 3.0 3 3.0 3
[2,] 3.5 3 2.5 2 2.5 3 3.5 4
我们发现,其实MARGIN并不是传递给FUN的(当然我们查看源码也知道了),
我们发现,FUN被调用了8次(这个次数就是行数),而我们每次都传递了一行数据进去。
结合上面的源码,我们发现,apply接受MARGIN函数后,会通过一些列的转化,最终决定FUN被调用几次,每次传递哪个维度的数据给FUN,最后才是在apply中重新组合为一个合适的向量/数组(数组包含矩阵),而...才是每次真正的传递给FUN的参数。
例子4
> ma <- matrix(c(1:4, 1, 6:8), nrow = 2)
> ma
[,1] [,2] [,3] [,4]
[1,] 1 3 1 7
[2,] 2 4 6 8
> apply(ma, 1, table) #--> a list of length 2
[[1]]
1 3 7
2 1 1
[[2]]
2 4 6 8
1 1 1 1
> apply(ma, 1, stats::quantile) # 5 x n matrix with rownames
[,1] [,2]
0% 1 2.0
25% 1 3.5
50% 2 5.0
75% 4 6.5
100% 7 8.0
> dim(ma) == dim(apply(ma, 1:2, sum)) #判断是否相等
[1] TRUE TRUE
> ma
[,1] [,2] [,3] [,4]
[1,] 1 3 1 7
[2,] 2 4 6 8
apply(ma, 1:2, sum)其实1:2的意思相当于每次把一个元素传给sum了,我们可以自己编写个函数试验下是否是真的传递了8次。