slides_apply

slides_apply

ID:40961476

大小:114.90 KB

页数:6页

时间:2019-08-12

slides_apply_第1页
slides_apply_第2页
slides_apply_第3页
slides_apply_第4页
slides_apply_第5页
资源描述:

《slides_apply》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库

1、applyapplyisusedtoaevaluateafunction(oftenananonymousone)overthemarginsofanarray.ItismostoftenusedtoapplyafunctiontotherowsorcolumnsofamatrixItcanbeusedwithgeneralarrays,e.g.takingtheaverageofanarrayofmatricesItisnotreallyfasterthanwritingaloop,butitworksinoneline!TheRLanguagea

2、pply>str(apply)function(X,MARGIN,FUN,...)XisanarrayMARGINisanintegervectorindicatingwhichmarginsshouldbe“retained”.FUNisafunctiontobeapplied...isforotherargumentstobepassedtoFUNTheRLanguageapply>x<-matrix(rnorm(200),20,10)>apply(x,2,mean)[1]0.048682680.35743615-0.09104379[4]-0.

3、05381370-0.16552070-0.18192493[7]0.102857270.365192700.14898850[10]0.26767260>apply(x,1,sum)[1]-1.948433142.606011951.51772391[4]-2.803868163.73728682-1.69371360[7]0.023599323.91874808-2.39902859[10]0.48685925-1.77576824-3.34016277[13]4.041010090.465154291.83687755[16]4.3674469

4、02.219937892.60983764[19]-1.486076303.58709251TheRLanguagecol/rowsumsandmeansForsumsandmeansofmatrixdimensions,wehavesomeshortcuts.rowSums=apply(x,1,sum)rowMeans=apply(x,1,mean)colSums=apply(x,2,sum)colMeans=apply(x,2,mean)Theshortcutfunctionsaremuchfaster,butyouwon’tnoticeunle

5、ssyou’reusingalargematrix.TheRLanguageOtherWaystoApplyQuantilesoftherowsofamatrix.>x<-matrix(rnorm(200),20,10)>apply(x,1,quantile,probs=c(0.25,0.75))[,1][,2][,3][,4]25%-0.3304284-0.99812467-0.9186279-0.4971168675%0.92581570.070657240.3050407-0.06585436[,5][,6][,7][,8]25%-0.0599

6、9553-0.6588380-0.6532500.0174999775%0.529287430.37274491.2550890.72318419[,9][,10][,11][,12]25%-1.2467955-0.8378429-1.0488430-0.705490275%0.33523770.72971760.31134340.4581150[,13][,14][,15][,16]25%-0.1895108-0.5729407-0.5968578-0.951706975%0.53262990.50642670.49338520.8868922[,

7、17][,18][,19][,20]TheRLanguage25%-0.2502935-0.7488003-0.7190923-0.63824375%0.77630240.28732020.64163631.271602applyAveragematrixinanarray>a<-array(rnorm(2*2*10),c(2,2,10))>apply(a,c(1,2),mean)[,1][,2][1,]-0.2353245-0.03980211[2,]-0.33397480.04364908>rowMeans(a,dims=2)[,1][,2][1

8、,]-0.2353245-0.03980211[2,]-0.33397480.04364908TheRLan

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
相关文章
更多
相关标签