ebook img

The Book of R: A First Course in Programming and Statistics [Chapters 2-12 ONLY] PDF

256 Pages·2016·4.135 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Book of R: A First Course in Programming and Statistics [Chapters 2-12 ONLY]

No Starch preSS e arly acceSS progr am: Feedback welcome! Welcome to the Early Access edition of the as yet unpublished Book of R by Tilman M. Davies! As a prepublication title, this book may be incomplete and some chapters may not have been proofread. Our goal is always to make the best books possible, and we look forward to hearing your thoughts. If you have any comments or questions, email us at [email protected]. If you have specific feedback for us, please include the page number, book title, and edition date in your note, and we’ll be sure to review it. We appreciate your help and support! We’ll email you as new chapters become available. In the meantime, enjoy! the b ook oF r tilmaN m . d avieS Early Access edition, 12/16/15 Copyright © 2015 by Tilman M. Davies. ISBN-10: 1-59327-651-6 ISBN-13: 978-1-59327-651-5 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Developmental Editors: Seph Kramer and Liz Chadwick Technical Reviewer: Debbie Leader Copyeditor: Kim Wimpsett Compositor: Riley Hoffman Proofreader: Paula L. Fleming No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trade- marked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any informa- tion storage or retrieval system, without the prior written permission of the copyright owner and the publisher. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. BRIEF CONTENTS Preface.............................................................................. xxi Introduction..........................................................................xxiii PARTI:THELANGUAGE Chapter1:GettingStarted ............................................................ 3 Chapter2:Numerics,Arithmetic,Assignment,andtheVector............................. 15 Chapter3:Matrices .................................................................. 37 Chapter4:Non-numericValues........................................................ 57 Chapter5:ListsandDataFrames ...................................................... 87 Chapter6:SpecialValues,Classes,andCoercion.......................................103 Chapter7:BasicPlotting..............................................................129 Chapter8:ReadingandWritingFiles..................................................149 PARTII:PROGRAMMING Chapter9:CallingFunctions ..........................................................167 Chapter10:ConditionsandLoops.....................................................181 Chapter11:WritingFunctions.........................................................217 Chapter12:Exceptions,Timings,andVisibility..........................................243 PARTIII:STATISTICSANDPROBABILITY Chapter13:ElementaryStatistics ......................................................263 Chapter14:BasicDataVisualization ..................................................291 Chapter15:Probability...............................................................311 Chapter16:CommonProbabilityDistributions ..........................................333 PARTIV:STATISTICALTESTINGANDMODELING Chapter17:SamplingDistributionsandConfidence.....................................369 Chapter18:HypothesisTesting........................................................389 Chapter19:AnalysisofVariance......................................................439 The chapters in red are included in this Early Access PDF. Chapter20:SimpleLinearRegression..................................................455 Chapter21:MultipleLinearRegression.................................................489 Chapter22:LinearModelSelectionandDiagnostics ....................................531 PARTV:ADVANCEDGRAPHICS Chapter23:AdvancedPlotCustomization..............................................579 Chapter24:GoingFurtherwiththeGrammarofGraphics ...............................613 Chapter25:DefiningColorsandPlottinginHigherDimensions...........................635 Chapter26:Interactive3DPlots .......................................................685 APPENDICES AppendixA:InstallingRandContributedPackages......................................723 AppendixB:WorkingwithRStudio ....................................................733 Bibliography.........................................................................741 viii BriefContents CONTENTS IN DETAIL 2 NUMERICS, ARITHMETIC, ASSIGNMENT, AND THE VECTOR 15 2.1 RforBasicMath............................................................ 15 2.1.1 Arithmetic...................................................... 16 2.1.2 LogarithmsandExponentials..................................... 17 2.1.3 E-Notation ..................................................... 18 2.2 AssigningVariablesandObjects............................................. 19 2.3 Vectors.................................................................... 21 2.3.1 CreatingaVector............................................... 21 2.3.2 Sequences,Repetition,Sorting,andLengths....................... 22 2.3.3 SubsettingandElementExtraction ................................ 26 2.3.4 Vector-OrientedBehavior........................................ 31 3 MATRICES 37 3.1 DefiningaMatrix........................................................... 37 3.1.1 FillingDirection................................................. 38 3.1.2 RowandColumnBindings....................................... 39 3.2 Subsetting ................................................................. 40 3.2.1 Row,Column,andDiagonalExtractions .......................... 41 3.2.2 OmittingandOverwriting ....................................... 42 3.3 MatrixOperationsandAlgebra.............................................. 45 3.3.1 MatrixTranspose ............................................... 45 3.3.2 IdentityMatrix.................................................. 46 3.3.3 ScalarMultipleofaMatrix ...................................... 47 3.3.4 MatrixAdditionandSubtraction ................................. 47 3.3.5 MatrixMultiplication ............................................ 48 3.3.6 MatrixInversion ................................................ 49 3.4 MultidimensionalArrays..................................................... 50 3.4.1 Definition ...................................................... 51 3.4.2 Subsets,Extractions,andReplacements ........................... 53 4 NON-NUMERIC VALUES 57 4.1 LogicalValues.............................................................. 57 4.1.1 TRUEorFALSE? ................................................ 58 4.1.2 ALogicalOutcome: RelationalOperators ......................... 59 4.1.3 MultipleComparisons: LogicalOperators ......................... 63 4.1.4 LogicalsAreNumbers! .......................................... 65 4.1.5 LogicalSubsettingandExtraction................................. 66 4.2 Characters................................................................. 70 4.2.1 CreatingaString ............................................... 71 4.2.2 Concatenation.................................................. 72 4.2.3 EscapeSequences .............................................. 74 4.2.4 SubstringsandMatching ........................................ 75 4.3 Factors .................................................................... 77 4.3.1 IdentifyingCategories........................................... 77 4.3.2 DefiningandOrderingLevels .................................... 80 4.3.3 CombiningandCutting.......................................... 81 5 LISTS AND DATA FRAMES 87 5.1 ListsofObjects ............................................................. 87 5.1.1 DefinitionandComponentAccess................................ 88 5.1.2 Naming ....................................................... 90 5.1.3 Nesting........................................................ 91 5.2 DataFrames ............................................................... 93 5.2.1 Construction.................................................... 94 5.2.2 AddingDataColumnsandCombiningDataFrames................ 96 5.2.3 LogicalRecordSubsets .......................................... 98 6 SPECIAL VALUES, CLASSES, AND COERCION 103 6.1 SomeSpecialValues........................................................103 6.1.1 Infinity.........................................................104 6.1.2 NaN ..........................................................106 6.1.3 NA............................................................108 6.1.4 NULL ..........................................................110 6.2 UnderstandingTypes,Classes,andCoercion .................................114 6.2.1 Attributes ......................................................114 6.2.2 ObjectClass ...................................................117 6.2.3 Is-DotObject-CheckingFunctions .................................120 6.2.4 As-DotCoercionFunctions.......................................121 7 BASIC PLOTTING 129 7.1 UsingplotwithCoordinateVectors...........................................129 7.2 GraphicalParameters.......................................................131 7.2.1 AutomaticPlotTypes ............................................131 7.2.2 TitleandAxisLabels ............................................132 7.2.3 Color..........................................................133 7.2.4 LineandPointAppearances .....................................135 x ContentsinDetail 7.2.5 PlottingRegionLimits............................................135 7.3 AddingPoints,Lines,andTexttoanExistingPlot ..............................136 7.4 Theggplot2Package .......................................................142 7.4.1 AQuickplotwithqplot..........................................142 7.4.2 SettingAppearanceConstantswithGeoms........................144 7.4.3 AestheticMappingwithGeoms ..................................146 8 READING AND WRITING FILES 149 8.1 R-ReadyDataSets ..........................................................149 8.1.1 Built-inDataSets................................................150 8.1.2 ContributedDataSets...........................................151 8.2 ReadinginExternalDataFiles ...............................................152 8.2.1 TheTableFormat ...............................................152 8.2.2 SpreadsheetWorkbooks ........................................155 8.2.3 Web-BasedFiles................................................156 8.2.4 OtherFileFormats ..............................................157 8.3 WritingOutDataFilesandPlots .............................................158 8.3.1 DataSets ......................................................158 8.3.2 PlotsandGraphicsFiles.........................................159 8.4 AdHocObjectRead/WriteOperations ......................................162 PART II PROGRAMMING 9 CALLING FUNCTIONS 167 9.1 Scoping ...................................................................167 9.1.1 Environments...................................................168 9.1.2 SearchPath ....................................................170 9.1.3 EnclosuresandtheEmptyEnvironment............................171 9.1.4 ReservedandProtectedNames ..................................172 9.2 ArgumentMatching.........................................................174 9.2.1 Exact ..........................................................174 9.2.2 Partial .........................................................175 9.2.3 Positional ......................................................176 9.2.4 Mixed .........................................................177 9.2.5 Dot-Dot-Dot: UseofEllipses ......................................178 10 CONDITIONS AND LOOPS 181 10.1 Whatif?...................................................................181 10.1.1 Stand-AloneStatement ..........................................182 ContentsinDetail xi 10.1.2 DoIt,orelse... .................................................185 10.1.3 UsingifelseforElement-wiseChecks..............................186 10.1.4 NestingandStackingStatements.................................188 10.1.5 TheOldswitch-eroo.............................................191 10.2 CodingLoops..............................................................195 10.2.1 forLoops.......................................................195 10.2.2 whileLoops ....................................................202 10.2.3 ImplicitLoopingwithapply ......................................206 10.3 OtherControlFlowMechanisms .............................................211 10.3.1 Declaringbreakornext .........................................211 10.3.2 NeedtorepeatIt? ..............................................213 11 WRITING FUNCTIONS 217 11.1 ThefunctionCommand......................................................217 11.1.1 FunctionCreation...............................................218 11.1.2 ToreturnorNottoreturn? .......................................221 11.2 Arguments.................................................................224 11.2.1 LazyEvaluation.................................................224 11.2.2 SettingDefaults.................................................227 11.2.3 CheckingforMissingArguments .................................228 11.2.4 DealingwithEllipses ............................................230 11.3 SpecializedFunctions.......................................................235 11.3.1 InternallyandExternallyDefinedHelperFunctions..................235 11.3.2 DisposableFunctions............................................237 11.3.3 RecursiveFunctions .............................................238 12 EXCEPTIONS, TIMINGS, AND VISIBILITY 243 12.1 ExceptionHandling.........................................................243 12.1.1 FormalNotifications: ErrorsandWarnings........................244 12.1.2 TryingExpressionsandCatchingErrors ...........................246 12.2 ProgressandTiming ........................................................251 12.2.1 TextualProgressBars: AreWeThereYet?.........................251 12.2.2 MeasuringCompletionTime: HowLongDidItTake?...............252 12.3 Masking...................................................................254 12.3.1 FunctionandObjectDistinction ..................................254 12.3.2 DataFrameVariableDistinction..................................257 xii ContentsinDetail

See more

The list of books you might like

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