Transcription of Hands-On Programming withR
1 DATA ANALYSIS/STATISTICAL SOFTWAREHa nd s - O n P rog ra m m i ng w it h RISBN: 978-1-449-35901-0US $ CAN $ Hands-On Programming w ith R is fr iend ly, conversational, and active. It s the nex t-best th i ng to lea r n i ng R Programming from me or Ga r ret t i n person. I hope you enjoy read i ng it as much as I have. Hadley WickhamChief Scientist at RStudioTwitter: how to program by diving into the R language, and then use your newfound skills to solve practical data science problems. With this book, you ll learn how to load data, assemble and disassemble data objects, navigate R s environment system, write your own functions, and use all of R s Programming Master Instructor Garrett Grolemund not only teaches you how to program, but also shows you how to get more from R than just visualizing and modeling data. You ll gain valuable Programming skills and support your work as a data scientist at the same time. Work Hands-On with three practical data analysis projects based on casino games Store, retrieve, and change data values in your computer s memory Write programs and simulations that outperform those written by typical R users Use R Programming tools such as if else statements, for loops, and S3 classes Learn how to write lightning-fast vectorized R code Take advantage of R s package system and debugging tools Practice and apply R Programming concepts as you learn themGarrett Grolemund is a statistician, teacher, and R developer who works as a data scientist and Master Instructor at RStudio.
2 Garrett received his PhD at Rice University, where his research traced the origins of data analysis as a cognitive process and identified how attentional and epistemological concerns guide every data nd s - O n P rog ra m m i ng w it h RGrolemundGarrett Grolemund Foreword by Hadley WickhamHands-On Programming w ith RWRITE YOUR OWN FUNCTIONS AND SIMULATIONSDATA ANALYSIS/STATISTICAL SOFTWAREHa nd s - O n P rog ra m m i ng w it h RISBN: 978-1-449-35901-0US $ CAN $ Hands-On Programming w ith R is fr iend ly, conversational, and active. It s the nex t-best th i ng to lea r n i ng R Programming from me or Ga r ret t i n person. I hope you enjoy read i ng it as much as I have. Hadley WickhamChief Scientist at RStudioTwitter: how to program by diving into the R language, and then use your newfound skills to solve practical data science problems. With this book, you ll learn how to load data, assemble and disassemble data objects, navigate R s environment system, write your own functions, and use all of R s Programming Master Instructor Garrett Grolemund not only teaches you how to program, but also shows you how to get more from R than just visualizing and modeling data.
3 You ll gain valuable Programming skills and support your work as a data scientist at the same time. Work Hands-On with three practical data analysis projects based on casino games Store, retrieve, and change data values in your computer s memory Write programs and simulations that outperform those written by typical R users Use R Programming tools such as if else statements, for loops, and S3 classes Learn how to write lightning-fast vectorized R code Take advantage of R s package system and debugging tools Practice and apply R Programming concepts as you learn themGarrett Grolemund is a statistician, teacher, and R developer who works as a data scientist and Master Instructor at RStudio. Garrett received his PhD at Rice University, where his research traced the origins of data analysis as a cognitive process and identified how attentional and epistemological concerns guide every data nd s - O n P rog ra m m i ng w it h RGrolemundGarrett Grolemund Foreword by Hadley WickhamHands-On Programming w ith RWRITE YOUR OWN FUNCTIONS AND SIMULATIONSG arrett GrolemundHands-On Programming with RHands-On Programming with Rby Garrett GrolemundCopyright 2014 Garrett Grolemund.
4 All rights in the United States of by O Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA Reilly books may be purchased for educational, business, or sales promotional use. Online editions arealso available for most titles ( ). For more information, contact our corporate/institutional sales department: 800-998-9938 or Julie Steele and Courtney NashProduction Editor: Matthew HackerCopyeditor: Eliahu SussmanProofreader: Amanda KerseyIndexer: Judith McConvilleCover Designer: Randy ComerInterior Designer: David FutatoIllustrator: Rebecca DemarestJuly 2014:First EditionRevision History for the First Edition:2014-07-08: First releaseSee for release Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks of O ReillyMedia, Inc. Hands-On Programming with R, the picture of an orange-winged Amazon parrot, and relatedtrade dress are trademarks of O Reilly Media, of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks.
5 Where those designations appear in this book, and O Reilly Media, Inc. was aware of a trademarkclaim, the designations have been printed in caps or initial every precaution has been taken in the preparation of this book, the publisher and authors assumeno responsibility for errors or omissions, or for damages resulting from the use of the information : 978-1-449-35901-0[LSI]Table of ContentsForeword.. viiPreface.. ixPart I. Project 1: Weighted Very Basics.. 3 The R User Interface 3 Objects 7 Functions 12 Sample with Replacement 14 Writing Your Own Functions 16 The Function Constructor 17 Arguments 18 Scripts
6 20 Summary and Help Pages.. 23 Packages 24library 24 Getting Help with Help Pages 29 Parts of a Help Page 30 Getting More Help 33 Summary 33 Project 1 Wrap-up
7 34iiiPart II. Project 2: Playing Objects.. 37 Atomic Vectors 38 Doubles 39 Integers 40 Characters 41 Logicals 42 Complex and Raw 42 Attributes 43 Names
8 44 Dim 45 Matrices 46 Arrays 46 Class 47 Dates and Times 48 Factors 49 Coercion 51 Lists 53 Data Frames 55 Loading Data 57 Saving Data 61 Summary Notation.
9 65 Selecting Values 65 Positive Integers 66 Negative Integers 68 Zero 69 Blank Spaces 69 Logical Values 69 Names 70 Deal a Card
10 70 Shuffle the Deck 71 Dollar Signs and Double Brackets 73 Summary Values.. 77 Changing Values in Place 77 Logical Subsetting 80 Logical Tests 80 Boolean Operators 85iv | Table of ContentsMissing Information 90 Summary