教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 小学教育 >

apply函数及其源码

来源:网络收集 时间:2026-09-08
导读: 我自己写的,导出的时候,字体的大小格式稍微有点问题。也可在我的博客看到(博客园) 总结: 就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传递给FUN)。apply的返回值结果可能是向量,数组(含

我自己写的,导出的时候,字体的大小格式稍微有点问题。也可在我的博客看到(博客园)

总结:

就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传递给FUN)。apply的返回值结果可能是向量,数组(含矩阵)或列表(具体要根据条件分类讨论,但实际上我们没有必要,直接看一下返回的结果就知道是什么类型了)apply {base}

DescriptionReturns a vector or array or list of values obtained by applying a function to margins of an array ormatrix.通过对数组或者矩阵的一个维度使用函数,并返回列表或者数组、向量。

Usageapply(X, MARGIN, FUN, ...)

Arguments

X 数组,包括矩阵

MARGIN

a vector giving the subscripts(下标 )which the function will be applied over. E.g., for a matrix 1indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has nameddimnames, 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 namemust 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 environmentof 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 neededto 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 areset, so that (for example) factor results will be coerced to a character array.

在被维度设定下来之前,这些结果都是被as.vector函数转换,所以因子的结果通常被转换为字符数组。

例:

apply函数的源码(直接F2即可)

为了方便理解源码,我们使用一个特例

我自己写的,导出的时候,字体的大小格式稍微有点问题。也可在我的博客看到(博客园)

源码:

我自己写的,导出的时候,字体的大小格式稍微有点问题。也可在我的博客看到(博客园)

[ 2,][ 3,]

3 5

4 6#3, 2

4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 .

d i m ( n e w X )< -c ( p r o d ( d . c a l l ),d 2 )

a n s< -v e c t o r (" l i s t",d 2 )#创建一个包含两个组件的列表[[ 1]] N U L L[[ 2]] N U L L

5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 .

i f( l e n g t h ( d . c a l l )<2 L ){# d . c a l l= 3,不成立 i f( l e n g t h ( d n . c a l l ) ) d i m n a m e s ( n e w X )< -c ( d n . c a l l,l i s t ( N U L L ) ) f o r( ii n1 L: d 2 ){ t m p< -f o r c e A n d C a l l ( 1,F U N,n e w X[,i],. . . ) i f( ! i s . n u l l ( t m p ) ) a n s[[ i]]< -t m p}} e l s ef o r( ii n1 L: d 2 ){# d 2= 2#执行 t m p< -f o r c e A n d C a l l ( 1,F U N,a r r a y ( n e w X[,i],d . c a l l, d n . c a l l ),. . . )#传给a p p l y的要被处理的数据是在这里才被传递给F U N的 i f( ! i s . n u l l ( t m p ) )#判断是否为空 a n s[[ i]]< -t m p}

#此时a n s[[ 1]][ 1]3# n e w X第一列的均值[[ 2]][ 1]4

7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 .a n s . l i s t< -i s . r e c u r s i v e ( a n s[[ 1 L]] ) l . a n s< -l e n g t h ( a n s[[ 1 L]] ) a n s . n a m e s< -n a

m e s ( a n s[[ 1 L]] )

#[ 1]F A L S E#l . a n s= 1# a n s . n a m e s= N U L L

i f( ! a n s . l i s t )#成立 a n s . l i s t< -a n y ( l e n g t h s ( a n s )!=l . a n s )# l e n g t h s ( a n s )[ 1] 1 1即每个组件中的元素的个数#[ …… 此处隐藏:9967字,全部文档内容请下载后查看。喜欢就下载吧 ……

apply函数及其源码.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1543934.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)