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]5 7 9 11……..。
(共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[a<20]: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.如:=+()6.letters():产生字母序列letters[1:30]:a,b,c,d…..30个字母()选择(a):a数列里面最大数which(a==2):查找a数列中等于2的数,并返回该数所对应位置a[which(a==2)]:先查找查找a数列中等于2的数,并返回该数所对应位置,然后对应的数值举例:a<-c(1,3,5,7)> which(a>5)[1] 4> a[which(a>5)][1] 7()函数---反转举例:> a=1:10> rev(a)[1] 10 9 8 7 6 5 4 3 2 1()函数---升序排列举例:> a=c(1,4,2,5,3,5,4,7,4)> sort(a)[1] 1 2 3 4 4 4 5 5 7()函数---将数据转变成按行列排布举例:> a=1:20> matrix(a,nrow=5,ncol=4)[,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 2010.函数t()----矩阵行列反置举例:> matrix(a,nrow=5,ncol=4)[,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> t( matrix(a,nrow=5,ncol=4))[,1] [,2] [,3] [,4] [,5][1,] 1 2 3 4 5[2,] 6 7 8 9 10[3,] 11 12 13 14 15[4,] 16 17 18 19 2011. diag():矩阵对角元素向量或生成对角矩阵举例:> a=matrix(1:16,nrow=4,ncol=4)> a[,1] [,2] [,3] [,4][1,] 1 5 9 13[2,] 2 6 10 14[3,] 3 7 11 15[4,] 4 8 12 16> diag(a)[1] 1 6 11 16> diag(diag(a))[,1] [,2] [,3] [,4][1,] 1 0 0 0[2,] 0 6 0 0[3,] 0 0 11 0[4,] 0 0 0 1612.统计分布每一种分布有四个函数: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:学生化极差():矩阵求逆或解线性方程14. eigen():矩阵的特征值分解生成excel类似的数组举例:> a=c(1,3,5,6,7)> b=c(2,4,6,6,8)> x=(a,b)> xa b1 1 22 3 43 5 64 6 65 7 8> (x=('重量'=a,'运费'=b))重量运费1 1 22 3 43 5 64 6 65 7 816.画散点图 plot()(二)初级函数文件读取:举例:>(x=(""))V1 V2 V31 12 32 4 5 62.读取excel文件1)先将excel文件保存为prn文件> y<("",header=T)> yage high weight1 18 150 502 17 160 602)安装RODBC安装包,导入excle文件举例:> local({pkg<(sort(.package=TRUE)),graphics=T$+ if(nchar(pkg))library(pkg,Error: unexpected 'if' in:"local({pkg<(sort(.package=TRUE)),graphics=T$if"> library(RODBC)> z<-odbcConnectExcel("")> (w<-sqlFetch(z,"Sheet1"))age high weight1 18 150 502 17 160 603. 循环语句:for举例:> for(i in 1:20){a[i]=i*2+3}> a[1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 434.循环语句:while举例:> a[1]=5> i=1> while(a[i]<121){i=i+1;a[i]=a[i-1]+2}> a[1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39[19] 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75[37] 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111[55] 113 115 117 119 121():数据来源,可将txt编辑好的程序直接读取,并运行。
举例:> source("D:\\百度云\\")[1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39[19] 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75[37] 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111[55] 113 115 117 119 1216.综合举例题目:模拟产生统计专业同学的名单(学号区分),记录数学分析,线性代数,概率统计三科成绩,然后进行一些统计分析结果:> num=seq(1008001,1008100)> num[1] 1008001 1008002 1008003 1008004 1008005 1008006 1008007 1008008 1008009[10] 1008010 1008011 1008012 1008013 1008014 1008015 1008016 1008017 1008018[19] 1008019 1008020 1008021 1008022 1008023 1008024 1008025 1008026 1008027[28] 1008028 1008029 1008030 1008031 1008032 1008033 1008034 1008035 1008036[37] 1008037 1008038 1008039 1008040 1008041 1008042 1008043 1008044 1008045[46] 1008046 1008047 1008048 1008049 1008050 1008051 1008052 1008053 1008054[55] 1008055 1008056 1008057 1008058 1008059 1008060 1008061 1008062 1008063[64] 1008064 1008065 1008066 1008067 1008068 1008069 1008070 1008071 1008072[73] 1008073 1008074 1008075 1008076 1008077 1008078 1008079 1008080 1008081[82] 1008082 1008083 1008084 1008085 1008086 1008087 1008088 1008089 1008090[91] 1008091 1008092 1008093 1008094 1008095 1008096 1008097 1008098 1008099[100] 1008100> x1=round(runif(100,min=80,max=100))> x1[1] 83 97 100 90 95 91 93 91 97 87 89 86 94 99 94 97 89 83[19] 82 98 98 84 100 87 89 97 99 94 88 93 89 90 89 83 93 95[37] 90 81 94 95 82 94 94 96 82 94 90 91 83 96 97 93 98 81[55] 87 80 94 84 94 80 87 84 83 85 96 81 97 87 93 97 98 82[73] 85 90 89 90 88 89 98 88 81 81 96 85 98 87 96 91 86 98[91] 100 89 87 85 90 90 82 80 81 95>> x2=round(rnorm(100,mean=80,sd=7))> x2[1] 73 79 88 88 92 79 77 90 87 83 93 85 80 80 84 87 63 72 78 77 85 81 80 78[25] 79 73 73 90 82 83 80 86 79 74 83 88 90 83 79 83 68 69 71 85 83 86 82 67[49] 84 72 66 91 74 83 73 78 86 77 81 81 89 84 83 82 91 81 80 69 92 85 82 79[73] 88 69 80 71 89 79 82 71 81 67 82 72 87 78 87 84 81 94 75 80 71 86 78 75[97] 75 72 95 75> x3=round(rnorm(100,mean=83,sd=18))> x3[1] 71 64 84 97 56 71 72 72 96 85 93 80 80 110 78 118 55 85[19] 99 83 92 113 65 91 76 120 63 78 72 70 94 101 69 84 100 87[37] 103 114 77 70 73 54 89 78 66 83 46 113 68 94 89 115 80 105[55] 67 100 73 89 80 64 115 74 81 99 82 90 92 90 73 101 88 96[73] 101 80 100 64 74 121 81 94 72 55 78 88 60 103 70 63 96 72[91] 104 93 92 110 74 82 80 111 64 92> x3[which(x3>100)]=100> x3[1] 71 64 84 97 56 71 72 72 96 85 93 80 80 100 78 100 55 85[19] 99 83 92 100 65 91 76 100 63 78 72 70 94 100 69 84 100 87[37] 100 100 77 70 73 54 89 78 66 83 46 100 68 94 89 100 80 100[55] 67 100 73 89 80 64 100 74 81 99 82 90 92 90 73 100 88 96[73] 100 80 100 64 74 100 81 94 72 55 78 88 60 100 70 63 96 72[91] 100 93 92 100 74 82 80 100 64 92> x=(num,x1,x2,x3) –数据合并> xnum x1 x2 x31 1008001 83 73 712 1008002 97 79 643 1008003 100 88 844 1008004 90 88 976 1008006 91 79 717 1008007 93 77 728 1008008 91 90 729 1008009 97 87 9610 1008010 87 83 8511 1008011 89 93 9312 1008012 86 85 8013 1008013 94 80 8014 1008014 99 80 10015 1008015 94 84 7816 1008016 97 87 10017 1008017 89 63 5518 1008018 83 72 8519 1008019 82 78 9920 1008020 98 77 8321 1008021 98 85 9222 1008022 84 81 10023 1008023 100 80 6524 1008024 87 78 9125 1008025 89 79 7626 1008026 97 73 10027 1008027 99 73 6328 1008028 94 90 7829 1008029 88 82 7230 1008030 93 83 7031 1008031 89 80 9432 1008032 90 86 10033 1008033 89 79 6934 1008034 83 74 8435 1008035 93 83 10036 1008036 95 88 8737 1008037 90 90 10038 1008038 81 83 10039 1008039 94 79 7740 1008040 95 83 7041 1008041 82 68 7342 1008042 94 69 5443 1008043 94 71 8944 1008044 96 85 7845 1008045 82 83 6646 1008046 94 86 8347 1008047 90 82 4648 1008048 91 67 10050 1008050 96 72 9451 1008051 97 66 8952 1008052 93 91 10053 1008053 98 74 8054 1008054 81 83 10055 1008055 87 73 6756 1008056 80 78 10057 1008057 94 86 7358 1008058 84 77 8959 1008059 94 81 8060 1008060 80 81 6461 1008061 87 89 10062 1008062 84 84 7463 1008063 83 83 8164 1008064 85 82 9965 1008065 96 91 8266 1008066 81 81 9067 1008067 97 80 9268 1008068 87 69 9069 1008069 93 92 7370 1008070 97 85 10071 1008071 98 82 8872 1008072 82 79 9673 1008073 85 88 10074 1008074 90 69 8075 1008075 89 80 10076 1008076 90 71 6477 1008077 88 89 7478 1008078 89 79 10079 1008079 98 82 8180 1008080 88 71 9481 1008081 81 81 7282 1008082 81 67 5583 1008083 96 82 7884 1008084 85 72 8885 1008085 98 87 6086 1008086 87 78 10087 1008087 96 87 7088 1008088 91 84 6389 1008089 86 81 9690 1008090 98 94 7291 1008091 100 75 10092 1008092 89 80 9394 1008094 85 86 10095 1008095 90 78 7496 1008096 90 75 8297 1008097 82 75 8098 1008098 80 72 10099 1008099 81 95 64100 1008100 95 75 92> (x,file="D:\\百度云\\",=F,=F,sep="") -保存数据():数据合并-合成举例:>x=(num,x1,x2,x3)():将数据写入(保存)txt文档举例:(x,file="D:\\百度云\\",=F,=F,sep=" ")9. mean():均值举例:():列均值举例:> colMeans(x)num x1 x2 x3> colMeans(x)[c("x1","x2","x3")]x1 x2 x3():举例:>apply(x,2,mean)num x1 x2 x3> apply(x,2,max)num x1 x2 x31008100 100 95 100> apply(x,2,min)num x1 x2 x31008001 80 63 46> apply(x[c("x1","x2","x3")],1,sum)—求总分[1] 227 240 272 275 243 241 242 253 280 255 275 251 254 279 256 284 207 240 259 258[21] 275 265 245 256 244 270 235 262 242 246 263 276 237 241 276 270 280 264 250 248[41] 223 217 254 259 231 263 218 258 235 262 252 284 252 264 227 258 253 250 255 225[61] 276 242 247 266 269 252 269 246 258 282 268 257 273 239 269 225 251 268 261 253[81] 234 203 256 245 245 265 253 238 263 264 275 262 250 271 242 247 237 252 240 262> (apply(x[c("x1","x2")],1,sum))---求总分最大的人是第几个[1] 46> x$num[(apply(x[c("x1","x2")],1,sum))]---求总分最大的人num[1] 1008046第二讲():绘制直方图接上例:>hist(x$x1)():绘制散点图接上例:> plot(x1,x2)> plot(x$x1,x$x2)():列联函数> table(x$x1)-----各数出现频数80 81 82 83 85 86 87 88 896 6 4 6 8 3 5 4 190 91 92 93 94 95 96 97 983 5 5 7 104 25 799 1004 5():柱状图接上例:> barplot(table(x$x1))():饼图接上例:> pie(table(x$x1))():箱尾图接上例:> boxplot(x$x1,x$x2,x$x3)1)箱子的上下横线为样本的25%和75%分位数2)箱子中间的横线为样本的中位数3)上下延伸的直线称为尾线,尾线的尽头为最高值和最低值4)异常值():箱线图接上例:> boxplot(x$x1,x$x2,x$x3,col=c("red","green","blue"),notch=T)或者:> boxplot(x[2:4],col=c("red","green","blue"),notch=T)():水平放置箱尾图接上例:>boxplot(x$x1,x$x2,x$x3,horizontal=T)():星象图1)每个观测单位的数值表示为一个图形2)每个图的每个角表示一个变量,字符串类型会标注在图的下方3)角线的长度表达值的大小A:接上例:> stars(x[c("x1","x2","x3")])B: > stars(x[c("x1","x2","x3")],full=T,=T)—饼状分割C: > stars(x[c("x1","x2","x3")],full=F,=T)():脸谱图1)用五官的宽度和高度来描绘数值2)人对脸谱高度敏感和强记忆3)适合较少样本的情况接上例:> library(tcltk2)> library(aplpack)> faces(x[c("x1","x2","x3")])effect of variables:modified item Var"height of face " "x1""width of face " "x2""structure of face" "x3""height of mouth " "x1""width of mouth " "x2""smiling " "x3""height of eyes " "x1""width of eyes " "x2""height of hair " "x3""width of hair " "x1""style of hair " "x2""height of nose " "x3""width of nose " "x1""width of ear " "x2""height of ear " "x3"21.其他脸谱:> library(TeachingDemos)> faces2(x)22.Stem():茎叶图> stem(x$x1)The decimal point is at the |80 | 00000000000082 | 000000000084 | 0000000086 | 0000000088 | 0000090 | 0000000092 | 00000000000094 | 0000000000000096 | 000000098 | 00000000000100 | 00000图:qqnorm(),qqline(),1)可用于判断是否正态分布2)直线的斜率是标准差,截距是均值3)点的散布越接近直线,则越接近正态分布接上例:> qqnorm(x1)> qqline(x1)24.散点图加深:plot()> plot(x$x1,x$x2,main="数学分析与线性代数成绩的关系",xlab="数学",ylab="线性",xlim=c(0,100),ylim=c(0,100),xaxs="i",yaxs="i",col="red",pch=19)25.折线图:plot()():密度图> plot(density(rnorm(1000)))27.内置数集:mtcars,iris,示例:>iris> sunflowerplot(iris[,3:4],col="red",="gold")28.散点图集:pairs()遍历样本中全部的变量配对,画出二元图,直观地了解所有变量之间的关系示例:> pairs(iris[,1:4])2)> plot(iris[,1:4],main="因素关系",pch=19,col="red",cex=2)3)par函数> par(mfrow=c(3,1))> plot(x1,x2);plot(x2,x3);plot(x3,x1)29.绘图颜色:colors()>colors()30.绘图设备:():31:绘图参数:mai():32.三维散点图:scatterplot3d()> library(scatterplot3d)> scatterplot3d(x[2:4])33.地图:map()> map("state",interior=FALSE)> map("world",fill=TRUE,col=(10))21。