Download The Art of R Programming PDF by Norman Matloff
ebook img

The Art of R Programming

404 Pages·2011·4.51 MB·English

Preview The Art of R Programming

TTAAMMEE YYOOUURR DDAATTAA TT HH EE TT AA RR TT OO FF RR HH EE AA R is the world’s most popular language for developing • Interface R with C/C++ and Python for increased PPRROOGG RR AA MMMMIINNGG RR statistical software: Archaeologists use it to track the speed or functionality spread of ancient civilizations, drug companies use it TT • Find new packages for text analysis, image manipula- to discover which medications are safe and effective, tion, and thousands more and actuaries use it to assess financial risks and keep OO A T O U R O F S T A T I S T I C A L S O F T W A R E D E S I G N markets running smoothly. • Squash annoying bugs with advanced debugging FF techniques The Art of R Programming takes you on a guided tour RR of software development with R, from basic types Whether you’re designing aircraft, forecasting the N O R M A N M A T L O F F and data structures to advanced topics like closures, weather, or you just need to tame your data, The Art of PP recursion, and anonymous functions. No statistical R Programming is your guide to harnessing the power knowledge is required, and your programming skills of statistical computing. RR can range from hobbyist to pro. ABOUT THE AUTHOR OO Along the way, you’ll learn about functional and object- Norman Matloff is a professor of computer science GG oriented programming, running mathematical simulations, (and a former professor of statistics) at the University and rearranging complex data into simpler, more useful of California, Davis. His research interests include RR formats. You’ll also learn to: parallel processing and statistical regression, and AA • Create artful graphs to visualize complex data sets he is the author of several widely used web tutorials and functions on software development. He has written articles for MM the New York Times, the Washington Post, Forbes • Write more efficient code using parallel R and MM Magazine, and the Los Angeles Times, and he is the vectorization co-author of The Art of Debugging (No Starch Press). II NN GG THE FINEST IN GEEK ENTERTAINMENT™ www.nostarch.com M A T “I LIE FLAT.” $39.95 ($41.95 CDN) LO This book uses RepKover—a durable binding that won’t snap shut. STATISTICAL SOCOMPUTERS/MSHELVE IN: FF FSC LOGO FTWAREATHEMATICAL & THE ART OF R PROGRAMMING THE ART OF R PROGRAMMING A Tour of Statistical Software Design by Norman Matloff SanFrancisco THEARTOFRPROGRAMMING.Copyright©2011byNormanMatloff. Allrightsreserved.Nopartofthisworkmaybereproducedortransmittedinanyformorbyanymeans,electronic ormechanical,includingphotocopying,recording,orbyanyinformationstorageorretrievalsystem,withoutthe priorwrittenpermissionofthecopyrightownerandthepublisher. 1514131211 123456789 ISBN-10:1-59327-384-3 ISBN-13:978-1-59327-384-2 Publisher:WilliamPollock ProductionEditor:AlisonLaw CoverandInteriorDesign:OctopodStudios DevelopmentalEditor:KeithFancher TechnicalReviewer:HadleyWickham Copyeditor:MarilynSmith Compositors:AlisonLawandSerenaYang Proofreader:PaulaL.Fleming Indexer:BIMIndexing&ProofreadingServices Forinformationonbookdistributorsortranslations,pleasecontactNoStarchPress,Inc.directly: NoStarchPress,Inc. 38RingoldStreet,SanFrancisco,CA94103 phone:415.863.9900;fax:415.863.9950;[email protected];www.nostarch.com LibraryofCongressCataloging-in-PublicationData Matloff, Norman S. The art of R programming : tour of statistical software design / by Norman Matloff. p. cm. ISBN-13: 978-1-59327-384-2 ISBN-10: 1-59327-384-3 1. Statistics-Data processing. 2. R (Computer program language) I. Title. QA276.4.M2925 2011 519.50285'5133-dc23 2011025598 NoStarchPressandtheNoStarchPresslogoareregisteredtrademarksofNoStarchPress,Inc.Otherproductand companynamesmentionedhereinmaybethetrademarksoftheirrespectiveowners.Ratherthanuseatrademark symbolwitheveryoccurrenceofatrademarkedname,weareusingthenamesonlyinaneditorialfashionandtothe benefitofthetrademarkowner,withnointentionofinfringementofthetrademark. Theinformationinthisbookisdistributedonan“AsIs”basis,withoutwarranty.Whileeveryprecautionhasbeen takeninthepreparationofthiswork,neithertheauthornorNoStarchPress,Inc.shallhaveanyliabilitytoany personorentitywithrespecttoanylossordamagecausedorallegedtobecauseddirectlyorindirectlybytheinfor- mationcontainedinit. BRIEF CONTENTS Acknowledgments ...................................................................xvii Introduction......................................................................... xix Chapter1:GettingStarted ........................................................... 1 Chapter2:Vectors .................................................................. 25 Chapter3:MatricesandArrays....................................................... 59 Chapter4:Lists...................................................................... 85 Chapter5:DataFrames..............................................................101 Chapter6:FactorsandTables ........................................................121 Chapter7:RProgrammingStructures..................................................139 Chapter8:DoingMathandSimulationsinR ...........................................189 Chapter9:Object-OrientedProgramming..............................................207 Chapter10:Input/Output............................................................231 Chapter11:StringManipulation......................................................251 Chapter12:Graphics ...............................................................261 Chapter13:Debugging..............................................................285 Chapter14:PerformanceEnhancement: SpeedandMemory ............................305 Chapter15:InterfacingRtoOtherLanguages..........................................323 Chapter16:ParallelR ...............................................................333 AppendixA:InstallingR..............................................................353 AppendixB:InstallingandUsingPackages ............................................355 CONTENTS IN DETAIL ACKNOWLEDGMENTS xvii INTRODUCTION xix WhyUseRforYourStatisticalWork? .............................................. xix Object-OrientedProgramming ............................................ xvii FunctionalProgramming.................................................. xvii WhomIsThisBookFor? ..........................................................xviii MyOwnBackground............................................................. xix 1 GETTING STARTED 1 1.1 HowtoRunR .............................................................. 1 1.1.1 InteractiveMode................................................ 2 1.1.2 BatchMode.................................................... 3 1.2 AFirstRSession............................................................ 4 1.3 IntroductiontoFunctions..................................................... 7 1.3.1 VariableScope................................................. 9 1.3.2 DefaultArguments .............................................. 9 1.4 PreviewofSomeImportantRDataStructures .................................. 10 1.4.1 Vectors,theRWorkhorse........................................ 10 1.4.2 CharacterStrings ............................................... 11 1.4.3 Matrices ....................................................... 11 1.4.4 Lists ........................................................... 12 1.4.5 DataFrames ................................................... 14 1.4.6 Classes ........................................................ 15 1.5 ExtendedExample: RegressionAnalysisofExamGrades....................... 16 1.6 StartupandShutdown ...................................................... 19 1.7 GettingHelp............................................................... 20 1.7.1 Thehelp()Function.............................................. 20 1.7.2 Theexample()Function.......................................... 21 1.7.3 IfYouDon’tKnowQuiteWhatYou’reLookingFor ................. 22 1.7.4 HelpforOtherTopics ........................................... 23 1.7.5 HelpforBatchMode............................................ 24 1.7.6 HelpontheInternet ............................................. 24 2 VECTORS 25 2.1 Scalars,Vectors,Arrays,andMatrices ....................................... 26 2.1.1 AddingandDeletingVectorElements............................. 26 2.1.2 ObtainingtheLengthofaVector ................................. 27 2.1.3 MatricesandArraysasVectors .................................. 28 2.2 Declarations ............................................................... 28 2.3 Recycling.................................................................. 29 2.4 CommonVectorOperations ................................................. 30 2.4.1 VectorArithmeticandLogicalOperations ......................... 30 2.4.2 VectorIndexing................................................. 31 2.4.3 GeneratingUsefulVectorswiththe: Operator..................... 32 2.4.4 GeneratingVectorSequenceswithseq() .......................... 33 2.4.5 RepeatingVectorConstantswithrep()............................. 34 2.5 Usingall()andany()........................................................ 35 2.5.1 ExtendedExample: FindingRunsofConsecutiveOnes ............. 35 2.5.2 ExtendedExample: PredictingDiscrete-ValuedTimeSeries.......... 37 2.6 VectorizedOperations...................................................... 39 2.6.1 VectorIn,VectorOut............................................ 40 2.6.2 VectorIn,MatrixOut............................................ 42 2.7 NAandNULLValues ....................................................... 43 2.7.1 UsingNA...................................................... 43 2.7.2 UsingNULL .................................................... 44 2.8 Filtering ................................................................... 45 2.8.1 GeneratingFilteringIndices...................................... 45 2.8.2 Filteringwiththesubset()Function ................................ 47 2.8.3 TheSelectionFunctionwhich() ................................... 47 2.9 AVectorizedif-then-else: Theifelse()Function ................................. 48 2.9.1 ExtendedExample: AMeasureofAssociation ..................... 49 2.9.2 ExtendedExample: RecodinganAbaloneDataSet ................ 51 2.10 TestingVectorEquality...................................................... 54 2.11 VectorElementNames...................................................... 56 2.12 Moreonc()................................................................ 56 3 MATRICES AND ARRAYS 59 3.1 CreatingMatrices .......................................................... 59 3.2 GeneralMatrixOperations.................................................. 61 3.2.1 PerformingLinearAlgebraOperationsonMatrices................. 61 3.2.2 MatrixIndexing ................................................ 62 3.2.3 ExtendedExample: ImageManipulation .......................... 63 3.2.4 FilteringonMatrices ............................................ 66 3.2.5 ExtendedExample: GeneratingaCovarianceMatrix............... 69 viii ContentsinDetail

See more

Similar The Art of R Programming

Upgrade Premium
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.