www.it-ebooks.info www.it-ebooks.info Learn how to turn data into decisions. From startups to the Fortune 500, smart companies are betting on data-driven insight, seizing the opportunities that are emerging from the convergence of four powerful trends: n New methods of collecting, managing, and analyzing data n Cloud computing that offers inexpensive storage and flexible, on-demand computing power for massive data sets n Visualization techniques that turn complex data into images that tell a compelling story n Tools that make the power of data available to anyone Get control over big data and turn it into insight with O’Reilly’s Strata offerings. Find the inspiration and information to create new products or revive existing ones, understand customer behavior, and get the data edge. Visit oreilly.com/data to learn more. ©2011 O’Reilly Media, Inc. O’Reilly logo is a registered trademark of O’Reilly Media, Inc. www.it-ebooks.info www.it-ebooks.info Learning R Richard Cotton www.it-ebooks.info Learning R by Richard Cotton Copyright © 2013 Richard Cotton. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editor: Meghan Blanchette Indexer: WordCo Indexing Services Production Editor: Kristen Brown Cover Designer: Karen Montgomery Copyeditor: Rachel Head Interior Designer: David Futato Proofreader: Jilly Gagnon Illustrator: Rebecca Demarest September 2013: First Edition Revision History for the First Edition: 2013-09-06: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449357108 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning R, the image of a roe deer, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-35710-8 [LSI] www.it-ebooks.info Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Part I. The R Language 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter Goals 3 What Is R? 3 Installing R 4 Choosing an IDE 5 Emacs + ESS 5 Eclipse/Architect 6 RStudio 6 Revolution-R 7 Live-R 7 Other IDEs and Editors 7 Your First Program 8 How to Get Help in R 8 Installing Extra Related Software 11 Summary 11 Test Your Knowledge: Quiz 12 Test Your Knowledge: Exercises 12 2. A Scientific Calculator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Chapter Goals 13 Mathematical Operations and Vectors 13 Assigning Variables 17 Special Numbers 19 Logical Vectors 20 Summary 22 v www.it-ebooks.info Test Your Knowledge: Quiz 22 Test Your Knowledge: Exercises 23 3. Inspecting Variables and Your Workspace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Chapter Goals 25 Classes 25 Different Types of Numbers 26 Other Common Classes 27 Checking and Changing Classes 30 Examining Variables 33 The Workspace 36 Summary 37 Test Your Knowledge: Quiz 37 Test Your Knowledge: Exercises 37 4. Vectors, Matrices, and Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter Goals 39 Vectors 39 Sequences 41 Lengths 42 Names 42 Indexing Vectors 43 Vector Recycling and Repetition 45 Matrices and Arrays 46 Creating Arrays and Matrices 46 Rows, Columns, and Dimensions 48 Row, Column, and Dimension Names 50 Indexing Arrays 51 Combining Matrices 51 Array Arithmetic 52 Summary 54 Test Your Knowledge: Quiz 55 Test Your Knowledge: Exercises 55 5. Lists and Data Frames. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Chapter Goals 57 Lists 57 Creating Lists 57 Atomic and Recursive Variables 60 List Dimensions and Arithmetic 60 Indexing Lists 61 Converting Between Vectors and Lists 64 vi | Table of Contents www.it-ebooks.info Combining Lists 65 NULL 66 Pairlists 70 Data Frames 70 Creating Data Frames 71 Indexing Data Frames 74 Basic Data Frame Manipulation 75 Summary 77 Test Your Knowledge: Quiz 77 Test Your Knowledge: Exercises 78 6. Environments and Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Chapter Goals 79 Environments 79 Functions 82 Creating and Calling Functions 82 Passing Functions to and from Other Functions 86 Variable Scope 89 Summary 91 Test Your Knowledge: Quiz 91 Test Your Knowledge: Exercises 91 7. Strings and Factors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Chapter Goals 93 Strings 93 Constructing and Printing Strings 94 Formatting Numbers 95 Special Characters 97 Changing Case 98 Extracting Substrings 98 Splitting Strings 99 File Paths 100 Factors 101 Creating Factors 101 Changing Factor Levels 103 Dropping Factor Levels 103 Ordered Factors 104 Converting Continuous Variables to Categorical 105 Converting Categorical Variables to Continuous 106 Generating Factor Levels 107 Combining Factors 107 Summary 108 Table of Contents | vii www.it-ebooks.info Test Your Knowledge: Quiz 108 Test Your Knowledge: Exercises 108 8. Flow Control and Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Chapter Goals 111 Flow Control 111 if and else 112 Vectorized if 114 Multiple Selection 115 Loops 116 repeat Loops 116 while Loops 118 for Loops 120 Summary 122 Test Your Knowledge: Quiz 122 Test Your Knowledge: Exercises 122 9. Advanced Looping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Chapter Goals 125 Replication 125 Looping Over Lists 127 Looping Over Arrays 132 Multiple-Input Apply 135 Instant Vectorization 136 Split-Apply-Combine 136 The plyr Package 138 Summary 141 Test Your Knowledge: Quiz 141 Test Your Knowledge: Exercises 141 10. Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Chapter Goals 143 Loading Packages 144 The Search Path 146 Libraries and Installed Packages 146 Installing Packages 148 Maintaining Packages 150 Summary 150 Test Your Knowledge: Quiz 151 Test Your Knowledge: Exercises 151 11. Dates and Times. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 viii | Table of Contents www.it-ebooks.info
Description: