R:Statistics?Programme?andWhoareYou?--AnABCintroductiontoRPresentedbyGuohuiDingR&D,SIBS,CASForFudanUniversityMainTopicsToday•WhatisR?•HowtoadministrateR?•HowdoesRwork?•HowtoapplyRforstatisticalproblem?•HowtoprogramyourRfunction?•………WhatisR?AbriefhistoryofRThelegendofR•Rstartedintheearly1990’sasaprojectbyRossIhakaandRobertGentlemanattheUniversityofAuckland,NewZealand,intendedtoprovideastatisticalenvironmentintheirteachinglab.ThelabhadMacintoshcomputers,forwhichnosuitablecommercialenvironmentwasavailable.RobertGentlemanRossIhakaR’sParents(1)•TheSlanguage–S:aninteractiveenvironmentfordataanalysisdevelopedatBellLaboratoriessince1976–ExclusivelylicensedbyAT&T/LucenttoInsightfulCorporation,SeattleWA.Productname:“S-plus”.Youcanlearnmorefrom:“R”?•TheSchemelanguageSchemeisastaticallyscopedandproperlytail-recursivedialectoftheLispprogramminglanguageinventedbyGuyLewisSteeleJr.andGeraldJaySussman.Learnmore:•Scheme’sunderlyingsemantics+S’syntax=RR’sParents(2)“WehavenamedourlanguageR–inparttoacknowledgetheinfluenceofSandinparttocelebrateourownefforts.”--R.IhakaR.Gentleman--IhakaR.&GentlemanR.,1996RNow•Sincemid-1997therehasbeenacoregroupwhocanmodifytheRsourcecodeCVSarchive.•TheRpackagesystemCRAN(theComprehensiveRArchiveNetwork)•Ris“GNUS”—Alanguageandenvironmentfordatamanipula-tion,calculationandgraphicaldisplay.–ThatisRisaFreeSoftware(orOpensourcesoftware).(Here,Freereferstofreedom,notprice,althoughRisfreeinthatsenseaswell.)•ThecoreofRisaninterpretedcomputerlanguage.–Amosaicofprocedure-basedprogrammingandobject-orientedprogramming–GoodinterfacetoprocedureswritteninC,C++,FORTRANandotherlanguages–Aflexibledataexchangemechanismaccessingrelationaldatabases-ODBC,PostgreSQL,MySQLandsoon.——小偷与强盗的谈判RandStatistics•Mostpackagesdealwithstatisticsanddataanalysis.•Powerfulstatisticalgraphics.•Wellcrosstalkingwithotherstatisticalsoftwares.•MostRuserarestatisticalexperts.Youcanlearnmoremodernanalysismethodfromtheybyemail.•Youcandoitwhenyoucomeacrossathingnobodydoitbefore.InstallandadministrateRFocusonWindows(MS)HowdoIgetR?•Theinformationalwebsite•CRAN-theComprehensiveRArchiveNetwork.–Theprimarysiteis–CRANsiteshavebinarydistributionsforWindows95,98,ME,NT4,2000andXPonIntel,fortheMacintosh(System8.6to9.1andMacOSX),andforseveralLinuxdistributions.•Newreleasesoccurfrequently–aboutevery3months.Bepreparedtore-installfrequently.•Alsoyoucangetitfromyourfriends,teachers,etc.Downit!Itisabout20.6Minsize.UsingPrecompiledBinaryDistributionsInstallingR•Doubleclick“rw1091.exe”usingyourmouse.ThatisOK.YoucaninstallitasallotherstandardMSsoftwares.RConsole/RGuiinWindows(MS)CommandboxGraphicsboxMenuIconsSeveralconceptsinAdministratingR•Workspace–xxx.RData•History–xxx.Rhistory•Package•Object•Session•ConsoleRunyourRcodesLoad/saveworkspaceLoad/saveHistoryChangeyourworkingdirectory--IhakaR.&GentlemanR.,1996Addanewpackage•Commands:–library()addapackageinthelibrary–detach(package:xxx)detachapackage•AllcandointheGUI(exceptdetach())LoadalocalpackageInstallpackagesfrominternetorlocalUpdatethelocalpackagefrominternetPackagesinREnvironment•Basicpackages–package:methodspackage:statspackage:graphics“package:utilspackage:base•Recommandedpackages–grid;lattice;e1071…•Contributedpackages(morethan366packagesnowadays)–……Youcanseewhatpackagesloadednowbythecommandsearch().Don’tloseyourway!•Threeusefulsystemcommand–getwd()GetWorkingDirectory–setwd()SetWorkingDirectory–list.files()ListtheFilesinaDirectory/FolderShowtheDemonstrationsofthePackages/Functions•Commands–demo()DemonstrationsofRFunctionality–example()RunanExamplesSectionfromtheOnlineHelpGettingHelps•Severalcommands–help.start()–help()or?()–help.search()–apropos()•Internetsearching–Ilikeitverymuch.Itseemsomnipotence.QuitR•Command–q()TerminateanRSessionHowdoesRwork?BasicRStructureanddatamanipulationBasicRworkingflow(Objectorientation)package--RforBeginners.EmmanuelParadisObjectorientation•Object:acollectionofatomicvariablesand/orotherobjectsthatbelongtogether•Parlance:–class:the“abstract”definitionofit–object:aconcreteinstance–method:otherwordfor‘function’–slot:acomponentofanobjectTypesofDatainR•Thebasicdataobjectisavectorofelementsoftype:–numericnumbers-eitherfloatingpointorinteger–charactereachelementisacharacterstring–logicaleachelementisTRUEorFALSE–listelementscanbeanytypeofobject,includingotherlists•ComponentsoftheSlanguage,suchasfunctions,arealsovectors.•AnyvectorcanincludethemissingdatamarkerNAasanelement.•Allvectorshavealengthandamode.Thefunctionslengthandmodereturnthisinformationasdoesthestrfunction.•Astructureconsistsofadataobjectplusadditionalinformation.Matrices(orarrays,ingeneral)andtimeseriesareexamplesofstructures.OperatorsVectors,MatricesandArrays•Command:–array(data=NA,dim=length(data),dimnames=NULL)–matrix(data=NA,nrow=1,ncol=1,byrow=