資源描述:
《the r book tables》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在工程資料-天天文庫。
1、6TablesThealternativetousinggraphicsistosummarizeyourdataintabularform.Broadlyspeaking,ifyouwanttoconveydetailuseatable,andifyouwanttoshoweffectsthenusegraphics.Youaremorelikelytowanttouseatabletosummarizedatawhenyourexplanatoryvariablesarecategorical(suc
2、haspeople’snames,ordifferentcommodities)thanwhentheyarecontinuous(inwhichcaseascatterplotislikelytobemoreinformative;seep.189).Therearetwoveryimportantfunctionsthatyouneedtodistinguish:tableforcountingthings;tapplyforaveragingthings,andapplyingotherfunc
3、tionsacrossfactorlevels.6.1TablesofcountsThetablefunctionisperhapsthemostusefulofallthesimplevectorfunctions,becauseitdoessomuchworkbehindthescenes.Wehaveavectorofobjects(theycouldbenumbersorcharacterstrings)andwewanttoknowhowmanyofeachispresentinthevecto
4、r.Hereare1000integersfromaPoissondistributionwithmean0.6:counts<-rpois(1000,0.6)Wewanttocountupallofthezeros,ones,twos,andsoon.Abigtask,buthereisthetablefunctioninaction:table(counts)counts0123455393251102411Therewere539zeros,325ones,110twos,24threes,1fou
5、r,1?veandnothinglargerthan5.Thatisalotofwork(imaginetallyingthemforyourself).Thefunctionworksforcharactersaswellasfornumbers,andforTheRBook,SecondEdition.MichaelJ.Crawley.?2013JohnWiley&Sons,Ltd.Published2013byJohnWiley&Sons,Ltd.TABLES245multipleclassifyi
6、ngvariables:infections<-read.table("c:\temp\disease.txt",header=T)attach(infections)head(infections)statusgender1clearmale2clearmale3clearmale4clearmale5clearmale6clearmaleandsoonfor1000rows.Youwanttoknowhowmanymalesandfemaleswereinfectedandhowmanywerec
7、learofinfection:table(status,gender)genderstatusfemalesmaleclear284515infected5368Ifyouwantthegendersastherowsratherthanthecolumns,thenputgender?rstintheargumentlisttotable:table(gender,status)statusgenderclearinfectedfemales28453male51568Thetablefunction
8、islikelytobeoneoftheRfunctionsyouusemostofteninyourownwork.6.2SummarytablesThemostimportantfunctioninRforgeneratingsummarytablesisthesomewhatobscurelynamedtapplyfunction.Itiscalledtapplybecauseitappliesanamedfunctio