R语言入门第十章、十一章、十二章.ppt

上传人:牧羊曲112 文档编号:6518026 上传时间:2023-11-08 格式:PPT 页数:22 大小:323.97KB
返回 下载 相关 举报
R语言入门第十章、十一章、十二章.ppt_第1页
第1页 / 共22页
R语言入门第十章、十一章、十二章.ppt_第2页
第2页 / 共22页
R语言入门第十章、十一章、十二章.ppt_第3页
第3页 / 共22页
R语言入门第十章、十一章、十二章.ppt_第4页
第4页 / 共22页
R语言入门第十章、十一章、十二章.ppt_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《R语言入门第十章、十一章、十二章.ppt》由会员分享,可在线阅读,更多相关《R语言入门第十章、十一章、十二章.ppt(22页珍藏版)》请在三一办公上搜索。

1、第十章输入与输出,1、输出到显示器scan(file=,what=double,sep=)函数:可以从文件中读取或者用键盘键入一个向量。可以看出,scan()的默认模式为double,默认间隔符为空格,使用时可根据具体情况修改,但遇到字符和数值混合的数据框,scan()就只能把所有内容统一为字符格式。scan(1.txt)Read 8 items1 1 21 2 22 3 23 4 24 scan(a.txt)#a中内容不是数值,出错Error in scan(file,what,nmax,sep,dec,quote,skip,nlines,na.strings,:scan()expected

2、 a real,got a scan(a.txt,what=)#把模式改为字符型,即可输出Read 3 items1 a bc de,此外,scan()可以从键盘读取数据,但似乎作用不大v inputsh1 hello,cat(.,file=,sep=):既有输出功能,也有写入功能,但不支持列表和数据框,而矩阵则被转化为向量。输出时要注意设置换行,cat没有自动换行功能。当然,print(x)也有输出功能,并有计算表达式值的功能ha ha1 a bc print(ha)1 a bc x print(x2)1 1 4 9,2、读取文本文件(读出来的都是数据框)read.table(file=,h

3、eader=FALSE):txt格式read.csv(file=,header=FALSE):csv格式,表格excel也可以读取,不过相对麻烦 需要安装RODBC和XML包,然后用channel-odbcConnectExcel(file=)建立连接,最后用sqlFetch(channel,SheetXX)读取source(file=.R),读取脚本文件,R也可以读取远程计算机上的文件uci uci ecc head(ecc)X11 X0 X71 X0.1 X0.260 X9 X4.600 X14 X1 X1.1 name X1.2 X0.21 19 0 72 0 0.380 6 4.100

4、 14 1.700 0.588 name 1 02 16 0 55 0 0.260 4 3.420 14 1 1 name 1 03 57 0 60 0 0.253 12.062 4.603 16 1.450 0.788 name 1 04 19 1 57 0 0.160 22 5.750 18 2.250 0.571 name 1 05 26 0 68 0 0.260 5 4.310 12 1 0.857 name 1 06 13 0 62 0 0.230 31 5.430 22.5 1.875 0.857 name 1 0,3、写文件cat(x,file=,sep=),前面提过write.

5、table(x,file=),写入格式为txtwrite.csv(x,file=),写入格式为csv,第十一章字符串操作,R语言的核心是数值向量和矩阵,但在文本挖掘中,字符串有大量应用,因此,R中有大量字符串操作工具。,1、函数介绍grep(pattern,x)#在字符串向量x中搜索给定的字符串pattern,并返回索引。grep(Pole,c(Equa,North Pole,South Pole)1 2 3grep(the,scan(aa.txt,what=)#scan()读取的也是向量,不过当内容不是数值型时,要注意转化为字符串Read 7 items1 1 4 5,nchar(x)#返回

6、字符串长度,可一次返回多个但要注意用c,即连接a nchar(a)1 10 a nchar(a)1 9 b nchar(b)1 2 3 nchar(a,b)Error in nchar(a,b):invalid type argument nchar(c(a,b)1 9 2 3,paste(x,y,sep=)#拼接多个字符串,并返回一个长字符串paste(a,b,sep=)#b中有两个元素,直接拼接a、b会把a和b的两个元素分别拼接1 SoutnPoleab SoutnPolecde paste(a,xy,z,sep=)1 SoutnPolexyz paste(a,xy,z,sep=)1 So

7、utnPole xy z paste(a,xy,z,sep=,)1 SoutnPole,xy,z,sprintf(%.,.):按一定格式组合若干组件为字符串sprintf(%f,pi)#默认情况的双精度sprintf(%x.3f,pi)#保留三位小数,最多占x位,x为正表示空格在前sprintf(%d,5)#默认情况的整型sprintf(%1.2f,pi)1 3.14 sprintf(%.2f,pi)1 3.14 sprintf(%10.1f,pi)1 3.1 sprintf(%1.5f,pi)1 3.14159 sprintf(%d,5)1 5i-8s-sprintf(the square

8、of%d is%d,i,i2)s1 the square of 8 is 64,substr(x,start,stop):返回字符串x中注定位置范围上的子字符串substring(x,start,stop):类似substr,但更强大 substr(equator,3,5)1 uat substring(equator,3,5)1 uat substr(equator,3,5:7)1 uat substring(equator,3,5:7)1 uat uato uator,strsplit(x,split=)#根据x中的字符串split拆分x strsplit(2-27-2014,-)11 2

9、 27 2014regexpr(pattern,x)#在字符串x中检索pattern,返回与pattern匹配的第一个子字符串的起始字符位置regexpr(uate,equater)#匹配起始位置1 3attr(,match.length)#匹配长度,不匹配则返回-11 4attr(,useBytes)1 TRUE,gregexpr(pattern,x),功能类似regexpr,但更强大,能返回所有与pattenr匹配的字符串的起始位置gregexpr(iss,mississp)11 2 5attr(,match.length)1 3 3attr(,useBytes)1 TRUE regexp

10、r(iss,mississp)1 2attr(,match.length)1 3attr(,useBytes)1 TRUE,2、正则表达式ab#含有字母a或b的字符串.#英文句点(.),表示任意一个字符,当想只让其代表句点时,需用“”,但由于“”本身也有特殊属性,所以需要多加一个“”。grep(au,c(equa,North Pole,South Pole)1 1 3 grep(0.e,c(equa,North Pole,South Pole)integer(0)grep(o.e,c(equa,North Pole,South Pole)1 2 3 grep(.,c(equa,North.Po

11、le,South.Pole)1 1 2 3 grep(.,c(equa,North.Pole,South.Pole)1 2 3,第十二章绘图,plot(x,y,type=l,pch=19,main=xxx,xlab=yyy,xlim=c(a,b)函数plot(c(1,2,3),c(1,2,4)dev.new()#打开新的绘图窗口,windows()有相同功能plot(c(-3,3),c(-1,5),type=n,xlab=x,ylab=y)#type=n表示不添加元素x-c(1,2,3)y-c(1,3,8)plot(x,y)lmot-lm(yx)#求y对x的线性模型abline(lmot)#添

12、加线性模型的线lines(y)#把点连起来,point(x,y,pch=+)函数#在指定区域添加点e2-round(rnorm(100,mean=70,sd=40)e1-round(rnorm(100,mean=75,sd=42)plot(c(0,100),c(0,100),type=n,xlab=x,ylab=y)points(e2,e3,pch=+),legned()#添加图例x-seq(-pi,pi,len=65)plot(x,sin(x),type=l,ylim=c(-1.2,1.8),col=3,lty=2)points(x,cos(x),pch=3,col=4)lines(x,ta

13、n(x),type=b,lty=1,pch=4,col=6)title(legend(.,lty=c(2,-1,1),pch=c(NA,3,4),merge=TRUE),cex.main=1.1)#merge=T合并点线,cex.main设置台头类型,lty设置线宽legend(-1,1.9,c(sin,cos,tan),col=c(3,4,6),text.col=green4,lty=c(2,-1,4),pch=c(NA,3,4),merge=TRUE,bg=gray90)#bg设置背景色,text(xlab,ylab,)函数#在指定位置添加文字locator(n)#鼠标定位,告诉R,我将在

14、图中点击n次,而R会告诉我我点击位置的坐标.locator(n)配合legend()和text()可以为添加图例和文字提供很大便利polygom(c(),c(),col=)#在图中添加多边形,第一个参数指定各个定点横坐标,第二个指定纵坐标,方向是左下逆时针旋转。pdf(xxx.pdf)#保存图形到xxx.pdf,三维图形library(lattice)a-1:10b-1:15eg-expand.grid(x=a,y=b)#生成x和y任意组合的数据框eg$z-eg$x2+eg$x*eg$ywireframe(zx+y,eg)绘制三维图形的函数不只这个。除了基本的绘图工具plot外,还有很多油特色功能的绘图函数。,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号