R Programming LanguageR程式語言課件_第1頁
R Programming LanguageR程式語言課件_第2頁
R Programming LanguageR程式語言課件_第3頁
R Programming LanguageR程式語言課件_第4頁
R Programming LanguageR程式語言課件_第5頁
已閱讀5頁,還剩87頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

RProgrammingLanguage

R林建甫C.F.JeffLin,MD.PhD.臺北大學統(tǒng)計系助理教授臺北榮民總醫(yī)院生物統(tǒng)計顧問美國密西根大學生物統(tǒng)計博士3/24/20231JeffLin,MD.PhD.******R:物件導向程式語言

RasObjective-OrientedLanguage3/24/20232JeffLin,MD.PhD.******R:基本(RBasics)物件命名與指派(NamingandAssign)變數(shù)類型(TypeofVariables)缺失值(MissingValues)資料輸入與指派(AssignmentandInputData)函數(shù)(Functions)工作路徑(Workspace)歷史紀錄(History)3/24/20233JeffLin,MD.PhD.******物件指派<-指派=(避免使用)>xyz.vector<-c(1,2,3)3/24/20235JeffLin,MD.PhD.******物件Objects物件名稱(names)物件種類(TypeofVariables):向量,因子,陣列,矩陣,資料框架,時間序列,列表.(vector,factor,array,matrix,data.frame,ts,list)屬性(attributes)模式(mode):邏輯,整數(shù),倍精準度,單精準度,複數(shù),文字.(numeric,character,complex,logical)長度(Length):與物件的模式有關物件產(chǎn)生(creation):指派數(shù)值或空物件3/24/20237JeffLin,MD.PhD.******>a<-49>sqrt(a)[1]7>b<-"Thedogatemyhomework">sub("dog","cat",b)[1]"Thecatatemyhomework">x<-(1+1==3)>x[1]FALSE>as.character(b)[1]"FALSE"數(shù)值numeric文字與字串characterstring邏輯

logical基本模式變數(shù)命名與指派3/24/20238JeffLin,MD.PhD.******物件指派Assignment“<-”usedtoindicateassignment>x<-c(1,2,3,4,5,6,7)>x<-c(1:7)>x<-1:43/24/202310JeffLin,MD.PhD.******3/24/202311JeffLin,MD.PhD.******R:邏輯操作與關係比較操作== Equalto!= Notequalto< Lessthan> Greaterthan<= Lessthanorequalto>= Greaterthanorequaltois.na(x) Missing?& LogicalAND| LogicalOR! LogicalNOT3/24/202313JeffLin,MD.PhD.******缺失值NA,NaN,與NullNA或“NotAvailable”可用在許多模式(modes)–character,numeric,etc.NaN或“NotaNumber”只用在數(shù)值模式(numericmodes)NULL列表(lists)的長度為0(zerolength)3/24/202314JeffLin,MD.PhD.******缺失值MissingValuesNA“notavailable”

>x<-c(1,2,3,NA) >x+3

[1]456NA非數(shù)字“Notanumber” >log(c(0,1,2))

[1]-Inf0.00000000.6931472 >0/0

[1]NaN3/24/202316JeffLin,MD.PhD.******R:缺失值MissingValuesNA或“NotAvailable”NA不是0NA不是““(空格,或空字串)NA不是FALSE任何與NA的計算,可能或不可能產(chǎn)生NA>1+NA[1]NA>max(c(NA,4,7))[1]NA>max(c(NA,4,7),na.rm=T)[1]73/24/202317JeffLin,MD.PhD.******TypesofObjects向量Vector矩陣Matrix陣列Array列表List因子Factor時間序列Timeseries資料框架Dataframe函式Function>typeof(物件名稱)可已回傳物件型態(tài)3/24/202319JeffLin,MD.PhD.******Mode物件結構(模式)模式Mode原型模式AtomicMode: logical,numeric,complex或character遞迴型RecursiveModelist,graphics,function,expression,call..>mode()指令可以用來查看物件的模式3/24/202320JeffLin,MD.PhD.******物件長度(Length)長度Lengthvector:numberofelementsmatrix,array:productofdimensionslist:numberofcomponentsdataframe:numberofcolumns3/24/202321JeffLin,MD.PhD.******物件屬性(Attribute)屬性Attributes列位名(rowname),欄位名(columnname),維度(dimension).>s()>names()>str()3/24/202322JeffLin,MD.PhD.******向量Vector指包含相同``模式''的元素(element)組成序列.主要有6種基本模式(mode)logical,integer,double,single,complex,andcharacter.(邏輯,整數(shù),倍精準度,單精準度,複數(shù),文字).向量是具有相同基本類型的元素序列,大體相當於其他語言中的1-維度數(shù)列,在R中,單一數(shù)值(scalar)也可看成是長度為1的向量.3/24/202324JeffLin,MD.PhD.******向量Vector>a<-c(1,2,3)>a*2[1]246向量的產(chǎn)生最常用辦法是使用函式c(),它把若干個數(shù)值或字串組合為一個向量,3/24/202325JeffLin,MD.PhD.******向量運算操作算數(shù)操作(arithmeticoperator)符號包含+,-,*,/,^,%%,%/%,%*%,%o%,%x%等.通常其含意是對向量的每一個元素進行運算的``單元運算子''(unary)或``二元運算子''(binary),如同一般算數(shù)運用在向量.向運有“長度”,但不具有“維度”.3/24/202326JeffLin,MD.PhD.******向量Vectors>Mydata<-c(2,3.5,-0.2) #>Colors<-c(“Red”,“Green”,“Red”) #文字>x1<-25:30>x1[1]252627282930 #數(shù)字序列>Colors[2][1]“Green”

#單一元素>x1[3:5][1]272829

#多各元素3/24/202327JeffLin,MD.PhD.******3/24/202329JeffLin,MD.PhD.******3/24/202330JeffLin,MD.PhD.******向量Vectors

>x<-c(5.2,1.7,6.3) >log(x)

[1]1.64865860.53062831.8405496

>y<-1:5 >z<-seq(1,1.4,by=0.1) >y+z [1]2.06.4

>length(y) [1]5

>mean(y+z) [1]4.23/24/202331JeffLin,MD.PhD.******3/24/202332JeffLin,MD.PhD.******向量運算操作邏輯檢測抽出某一元素刪除元素>Mydata[1]23.5-0.2

>Mydata>0[1]TRUETRUEFALSE >Mydata[Mydata>0][1]23.5

>Mydata[-c(1,3)][1]3.5

3/24/202333JeffLin,MD.PhD.******向量運算操作>x<-c(5,-2,3,-7) >y<-c(1,2,3,4)*10 #對所有元素操作>y[1]10203040>sort(x) #重新排序[1]-7-235>order(x)[1]4231

#排順序後的位置>y[order(x)][1]40203010 #>rev(x) #反向[1]-73-253/24/202334JeffLin,MD.PhD.******c()&rev()>c(1,3,5,7)[1]1357>rev(c(1,3,5,7))[1]75313/24/202335JeffLin,MD.PhD.******length(),mode()&names()>x<-c(1,3,5,7)>length(x)[1]4>mode(x)[1]"numeric">names(x)NULL3/24/202336JeffLin,MD.PhD.******seq()seq()產(chǎn)生數(shù)字序列>seq(1:5)[1]12345>seq(5,1,by=-1)[1]54321>seq(5)[1]123453/24/202337JeffLin,MD.PhD.******seq()>1.1:5[1]4.1>4:-5[1]43210-1-2-3-4-5>seq(-1,2,0.5)[1]-1.0-0.50.00.51.01.52.0>seq(1,by=0.5,length=5)[1]1.01.52.02.53.03/24/202338JeffLin,MD.PhD.******rep()rep()replicateselements>rep(1,5)[1]11111>rep(1:2,3)[1]121212>rep(1:2,each=3)[1]111222>rep(1:2,each=3,len=4)[1]1112>rep(1:2,each=3,len=7)[1]1112221>rep(1:2,each=3,time=2)[1]1112221112223/24/202339JeffLin,MD.PhD.******sort()&rank()>x<-c(8,6,9,7)>sort(x)[1]6789>rank(x)[1]3142>rank(x)[1][1]3>x[rank(x)==1][1]6>x[rank(x)][1]98763/24/202340JeffLin,MD.PhD.******rank()&order()>x<-c(8,6,9,7)>order(x)[1]2413>rank(x)[1]3142>x[order(x)][1]6789>x[rank(x)][1]98763/24/202341JeffLin,MD.PhD.******矩陣Matrix矩陣由包含相同的元素組成的2-維(2-dimension)資料物件可由matrix()產(chǎn)生>x<-matrix(data=0,nr=2,nc=2)>x<-matrix(0,2,2)3/24/202342JeffLin,MD.PhD.******矩陣下標

>x<-c("a","b","c","d","e","f","g","h") >x[1] >x[3:5] >x[-(3:5)] >x[c(T,F,T,F,T,F,T,F)] >x[x<="d"] >m[,2] >m[3,]3/24/202343JeffLin,MD.PhD.******GenerateaMatrix>xmat<-matrix(1:12,nrow=3,byrow=T)>xmat[,1][,2][,3][,4][1,]1234[2,]5678[3,]9101112>length(xmat)[1]12>dim(xmat)[1]34>mode(xmat)[1]"numeric">names(xmat)NULL>dimnames(xmat)NULL3/24/202344JeffLin,MD.PhD.******GenerateaMatrix>matrix(0,3,3)[,1][,2][,3][1,]000[2,]000[3,]0003/24/202345JeffLin,MD.PhD.******GenerateaMatrix>dimnames(xmat)<-list(c("A","B","C"),c("W","X","Y","Z"))>dimnames(xmat)[[1]][1]"A""B""C"[[2]][1]"W""X""Y""Z">xmatWXYZA1234B5678C91011123/24/202346JeffLin,MD.PhD.******DiagonalElementofaMatrix>m<-matrix(1:12,4,byrow=T)>m[,1][,2][,3][1,]123[2,]456[3,]789[4,]101112>diag(m)[1]1593/24/202347JeffLin,MD.PhD.******DiagonalElementofaMatrix>diag(k)[,1][,2][,3][1,]100[2,]010[3,]0013/24/202348JeffLin,MD.PhD.******InverseofMatrices>m<-matrix(c(1,3,5,,9,11,13,15,19,21),3,byrow=T)>m[,1][,2][,3][1,]135[2,]91113[3,]151921>solve(m)[,1][,2][,3][1,]-0.50001.0000-0.5[2,]0.1875-1.68751.0[3,]0.18750.8125-0.53/24/202349JeffLin,MD.PhD.******rbind()&cbind()>x<-c(1,2,3)>y<-matrix(0,3,3)>rbind(y,x)[,1][,2][,3]000000000x123>cbind(y,x)x[1,]0001[2,]0002[3,]00033/24/202350JeffLin,MD.PhD.******Multiplication>x<-matrix(1:4,2,byrow=T)>y<-matrix(1:4,2,byrow=T)>x*y#elementwise[,1][,2][1,]14[2,]916>x%*%y#[,1][,2][1,]710[2,]15223/24/202351JeffLin,MD.PhD.******>x%o%x,,1,1[,1][,2][1,]12[2,]34,,2,1[,1][,2][1,]36[2,]912,,1,2[,1][,2][1,]24[2,]68,,2,2[,1][,2][1,]48[2,]12163/24/202352JeffLin,MD.PhD.******ArrayArraysaregeneralizedmatricesbyextendingthefunctiondim()tomorthantwodimensions.>xarr<-array(c(1:8,11:18,111:118),dim=c(2,4,3))#row,col,array>xarr,,1[,1][,2][,3][,4][1,]1357[2,]2468,,2[,1][,2][,3][,4][1,]11131517[2,]12141618,,3[,1][,2][,3][,4][1,]111113115117[2,]1121141161183/24/202353JeffLin,MD.PhD.******列表Lists3/24/202354JeffLin,MD.PhD.******列表Lists列表是一個特殊的``向量'',這特殊的向量中的元素是物件.因此列表物件是由資料物件有順序組成,列表物中的``元素'',稱作``成份''(component)是物件本身.是有順序的(ordersequence).成份物件的元素模式,沒有任合限制,每一個別成份的物件之原型模式可以不相同.3/24/202355JeffLin,MD.PhD.******列表Lists>doe<-list(name="john",age=28,married=F)>doe$name[1]"john“>doe$age[1]28>doe[[3]][1]FALSE列表由“$”抽取元素3/24/202356JeffLin,MD.PhD.******列表Listsvector:

指包含相同``模式''的元素(element)組成序列.>a=c(7,5,1)>a[2][1]5list:列表是一個特殊的``向量'',這特殊的向量中的元素是物件.

>doe=list(name="john",age=28,married=F)>doe$name[1]"john“>doe$age[1]283/24/202357JeffLin,MD.PhD.******列表Lists>my.list<-list(c(5,4,-1),c("X1","X2","X3"))>my.list[[1]]:[1]54-1[[2]]:[1]"X1""X2""X3">my.list[[1]][1]54-1>my.list<-list(c1=c(5,4,-1),c2=c("X1","X2","X3"))>my.list$c2[2:3][1]"X2""X3"3/24/202358JeffLin,MD.PhD.******列表Lists>x.mat

[,1][,2][1,]3-1[2,]20[3,]-36>dimnames(x.mat)<-list(c("L1","L2","L3"),c("R1","R2"))>x.matR1R2L13-1L220L3-363/24/202359JeffLin,MD.PhD.******Lists,FactorsandDataFrames3/24/202360JeffLin,MD.PhD.******因數(shù)或因子Factorandfactor()處理類別資料,提供的一種有效的方法.因為統(tǒng)計中的離散變數(shù)(discretevariable)名義變數(shù)(nominalvariable)與有序變數(shù)(ordinalvariable).因素/因子是一種特殊的文字向量,文字向量中每一個元素,取一個離散值.因素物件有一個特殊屬性層次/水平/水準(levels)表示這組所有的離散值.因數(shù)可以簡單地用函式factor()產(chǎn)生.因素/因子是用``文字/字串‘’輸入,一但設定為因素/因子向量,{R}列印時,並不會加上雙引號{"}.3/24/202361JeffLin,MD.PhD.******因數(shù)或因子Factorandfactor()>gender<-c("male","female","male","male","female","female")>gender[1]"male""female""male""male""female""female">factor(gender)[1]malefemalemalemalefemalefemaleLevels:femalemale3/24/202362JeffLin,MD.PhD.******factor()andlevels()intensity<-factor(c("Hi","Med","Lo","Hi","Lo","Med", "Lo","Hi","Med"))>intensity[1]HiMedLoHiLoMedLoHiMedLevels:HiLoMed3/24/202363JeffLin,MD.PhD.******factor()andlevels()>intensity<-factor(c("Hi","Med","Lo","Hi","Lo","Med","Lo","Hi","Med"),levels=c("Hi","Med","Lo"))>intensity[1]HiMedLoHiLoMedLoHiMedLevels:HiMedLo3/24/202364JeffLin,MD.PhD.******factor()andlevels()intensity<-factor(c("Hi","Med","Lo","Hi","Lo","Med", "Lo","Hi","Med"),levels=c("Hi","Med","Lo"),labels=c("HiDOse","MedDOse","LoDose"))>intensity[1]HiDOseMedDOseLoDoseHiDOseLoDoseMedDOseLoDoseHiDOseMedDOseLevels:HiDOseMedDOseLoDose3/24/202365JeffLin,MD.PhD.******factor(),ordered()andlevels()intensity<-ordered(c("Hi","Med","Lo","Hi","Lo","Med", "Lo","Hi","Med"))>intensity[1]HiMedLoHiLoMedLoHiMedLevels:Hi<Lo<MedOooooop!Thisisnotwhatyouwant!3/24/202366JeffLin,MD.PhD.******factor(),ordered()andlevels()intensity<-ordered(c("Hi","Med","Lo","Hi","Lo","Med", "Lo","Hi","Med"),levels=c("Lo","Med","Hi"))>intensity[1]HiMedLoHiLoMedLoHiMedLevels:Lo<Med<HiOrdinalVariable!3/24/202367JeffLin,MD.PhD.******Lists,FactorsandDataFrames3/24/202368JeffLin,MD.PhD.******DataFramesdataframe:representsaspreadsheet.Rectangulartablewithrowsandcolumns;datawithineachcolumnhasthesametype(e.g.number,text,logical),butdifferentcolumnsmayhavedifferenttypes....3/24/202369JeffLin,MD.PhD.******DataFrames#RdataToothGrowth#TheEffectofVit.ConToothGrowthinGuineaPigs>ToothGrowthlensuppdose14.2VC0.5211.5VC0.537.3VC0.545.8VC0.5…5827.3OJ2.05929.4OJ2.06023.0OJ2.03/24/202370JeffLin,MD.PhD.******DataFramesAdataframeisalistwithclass“data.frame”.Therearerestrictionsonliststhatmaybemadeintodataframes.a.Thecomponentsmustbevectors(numeric,character,orlogical),factors,numericmatrices,lists,orotherdataframes.b.Matrices,lists,anddataframesprovideasmanyvariablestothenewdataframeastheyhavecolumns,elements,orvariables,respectively.3/24/202371JeffLin,MD.PhD.******DataFrames

c.Numericvectorsandfactorsareincludedasis,andnon-numericvectorsarecoercedtobefactors,whoselevelsaretheuniquevaluesappearinginthevector.d.Vectorstructuresappearingasvariablesofthedataframemustallhavethesamelength,andmatrixstructuresmustallhavethesamerowsize.3/24/202372JeffLin,MD.PhD.******DataFrameseveralmodesallowedwithinasingledataframecanbecreatedusingdata.frame()L<-LETTERS[1:4]#ABCDx<-1:4#1234data.frame(x,L)#createdataframeattach()anddetach()thedatabaseisattachedtotheRsearchpathsothatthedatabaseissearchedbyRwhenitisevaluatingavariable.objectsinthedatabasecanbeaccessedbysimplygivingtheirnames3/24/202373JeffLin,MD.PhD.******DataElementsselectonlyoneelementx[2]selectrangeofelementsx[1:3]selectallbutoneelementx[-3]slicing:includingonlypartoftheobjectx[c(1,2,5)]selectelementsbasedonlogicaloperatorx(x>3)3/24/202374JeffLin,MD.PhD.******Individualelementsofavector,matrix,arrayordataframeareaccessedwith“[]”byspecifyingtheirindex,ortheirname>ToothGrowth[1:3,]lensuppdose14.2VC0.5211.5VC0.57.3VC0.5>ToothGrowth[1:2,1:2]

lensupp14.2VC211.5VCSubsetting3/24/202375JeffLin,MD.PhD.******LabelsinDataFrames>labels(ToothGrowth)[[1]][1]"1""2""3""4""5""6""7""8""9""10""11""12"[13]"13""14""15""16""17""18""19""20""21""22""23""24"[25]"25""26""27""28""29""30""31""32""33""34""35""36"[37]"37""38""39""40""41""42""43""44""45""46""47""48"[49]"49""50""51""52""53""54""55""56""57""58""59""60"[[2]][1]"len""supp""dose"3/24/202376JeffLin,MD.PhD.******Findingoutaboutadataobjectmode():tellsyouthestorage‘mode’ofanobject(i.e.whetheritisanumericvector,oralistetc.)attributes():providesinformationaboutthedataobjectclass():providesinformaitonabouttheobject’sclass.Theclassofanobjectoftendetermineshowthedataobjectishandledbyafunction.Youcanalsosettheobject’smode,attributesorclassusingtheabovefunctions.e.g.mode(x)<-“numeric”3/24/202377JeffLin,MD.PhD.******Whattypeismydata?classClassfromwhichobjectinherits(vector,matrix,function,logical,list,…)modeNumeric,character,logical,…storage.modetypeofModeusedbyRtostoreobject(double,integer,character,logical,…)is.functionLogical(TRUEiffunction)is.naLogical(TRUEifmissing)namesNamesassociatedwithobjectdimnamesNamesforeachdimofarrayslotNamesNamesofslotsofBioCobjectsattributesNames,class,etc.3/24/202378JeffLin,MD.PhD.******DataImport&Entry3/24/202379JeffLin,MD.PhD.******TopicsDatasetsthatcomewithRInputtingdatafromafileWritingdatatoafileWritingdatatotheclipboardExchangingdatabetweenprogramsNB:savingtheworkspace3/24/202380JeffLin,MD.PhD.******Rcomeswithseveralpre-packageddatasets

Youcanaccessthesedatasetswiththedatafunctiondata()

getsyoualistofallthedatasetsdata(Titanic)

loadsadatasetaboutpassengers ontheTitanic(forexample)summary(Titanic)

providessomesummaryinformation aboutthedatasetTitanicattributes(Titanic)providessomemoreinformationTypingthedatasetnameonitsown(followedbyEnter)willdisplaythedata3/24/202381JeffLin,MD.PhD.******Data>summary(data)>names(data)>attributes(data)Editingdata>fix(data)or>edit(data)>data$var>attach(data)#inordertoremoveneedof‘$’>detach(data)3/24/202382JeffLin,MD.PhD.******DataEntry&Editingstarteditorandsavechangesdata.entry(x)starteditor,changesnotsavedde(x)starttexteditoredit(x)3/24/202383JeffLin,MD.PhD.******TheattachanddetachfunctionsTheattachfunctionmakesalltheobjectsinalistordataframeaccessiblefromoutsidethelistordataframe.E.g.insteadoftypingmy_list$agetoaccessthevector‘a(chǎn)ge’inthelistmy_listyoucanjusttype‘a(chǎn)ge’(providedthereisnoothervectorcalled‘a(chǎn)ge’inthemainworkspace).Thedetach

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論