您好,欢迎访问三七文档
R-note一、基本函数1.函数c()—向量,length()—长度,mode()—众数,rbind()—组合,cbind()—转置,mode()—属性(数值、字符等)2.函数mean()-中位数,sum()-求和,min()-最小值,max()-最大值,var()-方差,sd()-标准差,prod()–连乘3.函数help()--帮助4.正态分布函数rnorm()、泊松分布函数rpois()、指数分布函数rexp()、Gamma分布函数rgamma()、均匀分布函数runif()、二项分布函数rbinom()、几何分布函数rgeom()(一)基本函数1.2:60*2+1[1]57911……..。。。(共60个数)2.a[5]:a数列第5个数,a[-5]:删除a数列第5位数a[-(1:5)]:删除a数列第1-5位数a[c(2,4,7)]:a数列第2,4,7位数a[a20]:a数列小于20的数a[a[3]]:先查找a数列第3位数对应数值,然后找第该位数对应数值5.Seq()函数---序列数产生器Seq(5,20):产生5,6。。。。20的数集Seq(5,100,by=2):产生5开始,步长为2的数集,最大值为100Seq(5,100,length=10):产生从5开始,从第三个数开始等于第二个数加上第二个数减去第一个数的差值,最后一个数为100.5.0000011.7857118.5714325.3571432.1428638.9285745.7142952.5000059.2857166.0714372.8571479.6428686.4285793.21429100.00000如:18.57143=11.78571+(11.78571-5.00000)6.letters():产生字母序列letters[1:30]:a,b,c,d…..30个字母6.which()选择which.max(a):a数列里面最大数which(a==2):查找a数列中等于2的数,并返回该数所对应位置a[which(a==2)]:先查找查找a数列中等于2的数,并返回该数所对应位置,然后对应的数值举例:a-c(1,3,5,7)which(a5)[1]4a[which(a5)][1]77.rev()函数---反转举例:a=1:10rev(a)[1]109876543218.sort()函数---升序排列举例:a=c(1,4,2,5,3,5,4,7,4)sort(a)[1]1234445579.matrix()函数---将数据转变成按行列排布举例:a=1:20matrix(a,nrow=5,ncol=4)[,1][,2][,3][,4][1,]161116[2,]271217[3,]381318[4,]491419[5,]510152010.函数t()----矩阵行列反置举例:matrix(a,nrow=5,ncol=4)[,1][,2][,3][,4][1,]161116[2,]271217[3,]381318[4,]491419[5,]5101520t(matrix(a,nrow=5,ncol=4))[,1][,2][,3][,4][,5][1,]12345[2,]678910[3,]1112131415[4,]161718192011.diag():矩阵对角元素向量或生成对角矩阵举例:a=matrix(1:16,nrow=4,ncol=4)a[,1][,2][,3][,4][1,]15913[2,]261014[3,]371115[4,]481216diag(a)[1]161116diag(diag(a))[,1][,2][,3][,4][1,]1000[2,]0600[3,]00110[4,]0001612.统计分布每一种分布有四个函数:d――density(密度函数),p――分布函数,q――分位数函数,r――随机数函数。比如,正态分布的这四个函数为dnorm,pnorm,qnorm,rnorm。下面我们列出各分布后缀,前面加前缀d、p、q或r就构成函数名:norm:正态,t:t分布,f:F分布,chisq:卡方(包括非中心)unif:均匀,exp:指数,weibull:威布尔,gamma:伽玛,beta:贝塔lnorm:对数正态,logis:逻辑分布,cauchy:柯西,binom:二项分布,geom:几何分布,hyper:超几何,nbinom:负二项,pois:泊松signrank:符号秩,wilcox:秩和,tukey:学生化极差13.solve():矩阵求逆或解线性方程14.eigen():矩阵的特征值分解15.data.frame():生成excel类似的数组举例:a=c(1,3,5,6,7)b=c(2,4,6,6,8)x=data.frame(a,b)xab112234356466578(x=data.frame('重量'=a,'运费'=b))重量运费11223435646657816.画散点图plot()(二)初级函数1.txt文件读取:举例:(x=read.table(D://百度云/abc.txt))V1V2V3112324562.读取excel文件1)先将excel文件保存为prn文件y-read.table(D://百度云//a.prn,header=T)yagehighweight11815050217160602)安装RODBC安装包,导入excle文件举例:local({pkg-select.list(sort(.package(all.available=TRUE)),graphics=T$+if(nchar(pkg))library(pkg,character.onError:unexpected'if'in:local({pkg-select.list(sort(.package(all.available=TRUE)),graphics=T$iflibrary(RODBC)z-odbcConnectExcel(D://百度云//a.xls)(w-sqlFetch(z,Sheet1))agehighweight11815050217160603.循环语句:for举例:for(iin1:20){a[i]=i*2+3}a[1]57911131517192123252729313335373941434.循环语句:while举例:a[1]=5i=1while(a[i]121){i=i+1;a[i]=a[i-1]+2}a[1]579111315171921232527293133353739[19]414345474951535557596163656769717375[37]777981838587899193959799101103105107109111[55]1131151171191215.source():数据来源,可将txt编辑好的程序直接读取,并运行。举例:source(D:\\百度云\\a.txt)[1]579111315171921232527293133353739[19]414345474951535557596163656769717375[37]777981838587899193959799101103105107109111[55]1131151171191216.综合举例题目:模拟产生统计专业同学的名单(学号区分),记录数学分析,线性代数,概率统计三科成绩,然后进行一些统计分析结果:num=seq(1008001,1008100)num[1]100800110080021008003100800410080051008006100800710080081008009[10]100801010080111008012100801310080141008015100801610080171008018[19]100801910080201008021100802210080231008024100802510080261008027[28]100802810080291008030100803110080321008033100803410080351008036[37]100803710080381008039100804010080411008042100804310080441008045[46]100804610080471008048100804910080501008051100805210080531008054[55]100805510080561008057100805810080591008060100806110080621008063[64]100806410080651008066100806710080681008069100807010080711008072[73]100807310080741008075100807610080771008078100807910080801008081[82]100808210080831008084100808510080861008087100808810080891008090[91]100809110080921008093100809410080951008096100809710080981008099[100]1008100x1=round(runif(100,min=80,max=100))x1[1]8397100909591939197878986949994978983[19]8298988410087899799948893899089839395[37]908194958294949682949091839697939881[55]878094849480878483859681978793979882[73]859089908889988881819685988796918698[91]100898785909082808195x2=round(rnorm(100,mean=80,sd=7))x2[1]737988889279779087839385808084876372787785818078[25]797373908283808679748388908379836869718583868267[49]847266917483737886778181898483829181806992858279[73]886980718979827181678272877887848194758071867875[97]75729575x3=round(rnorm(100,mean=83,sd=18))x3[1]71648497567172729685938080110781185585[19]9983921136591761206378727094101698410087[37]10311477707354897866834611368948911580105[55]671007389806411574819982909290731018896[73]1018010064741218194725578886010370639672[91]10493921107482801116492x3[which(x3100)]=100x3[1]71648497567172729685938080100781005585[19]9983921006591761006378727094100698410087[37]10010077707354897866834610068948910080100[55]671007389806410074819982909290731008896[73]1008010064741008194725578886010070639672[91]10093921007
本文标题:R语句常用函数汇总
链接地址:https://www.777doc.com/doc-5475112 .html