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字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [小学教育]四年级综合实践活动课《衣物的洗涤》教
- [小学教育]2014半年工作总结怎么写
- [小学教育]20世纪外国文学专题综合试题及答案
- [小学教育]TS_1循环使用催化丙烯环氧化反应研究
- [小学教育]最实用的考勤签到表(上下班签到表)
- [小学教育]气候与生态建筑——以新疆民居为例
- [小学教育]二人以上股东有限责任公司章程参考样本
- [小学教育]2014届第一轮复习资料4.1,3美好生活的
- [小学教育]土方开挖、降水方案
- [小学教育]手绘儿童绘本《秋天的图画》(蜡笔)
- [小学教育]2002级硕士研究生卫生统计学考试试题
- [小学教育]环保装备重点发展目录
- [小学教育]金蝶K3合并报表培训教材
- [小学教育]岩浆岩试题及参考答案
- [小学教育]知之深爱之切学习心得
- [小学教育]第十二章 蛋白质的生物合成
- [小学教育]Chapter 2-3 Solid structure and basi
- [小学教育]市政道路雨季专项施工方案
- [小学教育]中国海洋大学2012-2013学年第二学期天
- [小学教育]教育心理学第3章-学习迁移
- 浅谈深化国企改革中加强党管企业
- 2006年中国病理生理学会学术活动安排
- 设计投标工作大纲
- 基于ARP的网络攻击与防御
- 2016届湖北省七市(州)教科研协作体高三
- Google_学术搜索及其检索技巧
- 2019-2020学年七年级地理下册6.3美洲教
- 城市道路可研报告
- 【名师指津】2012高考英语 写作基础技
- 6级知识点培训北京师范大学《幼儿智趣
- 注册会计师会计知识点:金融资产
- 新安装 500 kV 变压器介损分析与判断
- PS2模拟器PCSX2设置及使用教程.
- 医院药事管理与药剂科管理组织机构
- {PPT背景素材}丹巴的醉人美景,免费,一
- NAS网络存储应用解决方案
- 青海省西宁市六年级上学期数学期末考试
- 测量管理体系手册依据ISO10012:2003
- 洞子小学培养骨干教师工作计划
- 浅谈《牛津初中英语》的教材特点及教学




