logo资料库

The Art of R Programming.pdf

第1页 / 共404页
第2页 / 共404页
第3页 / 共404页
第4页 / 共404页
第5页 / 共404页
第6页 / 共404页
第7页 / 共404页
第8页 / 共404页
资料共404页,剩余部分请下载后查看
Copyright
Brief Contents
Contents in Detail
Acknowledgments
Introduction
Why Use R for Your Statistical Work?
Whom Is This Book For?
My Own Background
1: Getting Started
1.1 How to Run R
1.2 A First R Session
1.3 Introduction to Functions
1.4 Preview of Some Important R Data Structures
1.5 Extended Example: Regression Analysis of Exam Grades
1.6 Startup and Shutdown
1.7 Getting Help
2: Vectors
2.1 Scalars, Vectors, Arrays, and Matrices
2.2 Declarations
2.3 Recycling
2.4 Common Vector Operations
2.5 Using all() and any()
2.6 Vectorized Operations
2.7 NA and NULL Values
2.8 Filtering
2.9 A Vectorized if-then-else: The ifelse() Function
2.10 Testing Vector Equality
2.11 Vector Element Names
2.12 More on c()
3: Matrices and Arrays
3.1 Creating Matrices
3.2 General Matrix Operations
3.3 Applying Functions to Matrix Rows and Columns
3.4 Adding and Deleting Matrix Rows and Columns
3.5 More on the Vector/Matrix Distinction
3.6 Avoiding Unintended Dimension Reduction
3.7 Naming Matrix Rows and Columns
3.8 Higher-Dimensional Arrays
4: Lists
4.1 Creating Lists
4.2 General List Operations
4.3 Accessing List Components and Values
4.4 Applying Functions to Lists
4.5 Recursive Lists
5: Data Frames
5.1 Creating Data Frames
5.2 Other Matrix-Like Operations
5.3 Merging Data Frames
5.4 Applying Functions to Data Frames
6: Factors and Tables
6.1 Factors and Levels
6.2 Common Functions Used with Factors
6.3 Working with Tables
6.4 Other Factor- and Table-Related Functions
7: R Programming Structures
7.1 Control Statements
7.2 Arithmetic and Boolean Operators and Values
7.3 Default Values for Arguments
7.4 Return Values
7.5 Functions Are Objects
7.6 Environment and Scope Issues
7.7 No Pointers in R
7.8 Writing Upstairs
7.9 Recursion
7.10 Replacement Functions
7.11 Tools for Composing Function Code
7.12 Writing Your Own Binary Operations
7.13 Anonymous Functions
8: Doing Math and Simulations in R
8.1 Math Functions
8.2 Functions for Statistical Distributions
8.3 Sorting
8.4 Linear Algebra Operations on Vectors and Matrices
8.5 Set Operations
8.6 Simulation Programming in R
9: Object-Oriented Programming
9.1 S3 Classes
9.2 S4 Classes
9.3 S3 Versus S4
9.4 Managing Your Objects
10: Input/Output
10.1 Accessing the Keyboard and Monitor
10.2 Reading and Writing Files
10.3 Accessing the Internet
11: String Manipulation
11.1 An Overview of String-Manipulation Functions
11.2 Regular Expressions
11.3 Use of String Utilities in the edtdbg Debugging Tool
12: Graphics
12.1 Creating Graphs
12.2 Customizing Graphs
12.3 Saving Graphs to Files
12.4 Creating Three-Dimensional Plots
13: Debugging
13.1 Fundamental Principles of Debugging
13.2 Why Use a Debugging Tool?
13.3 Using R Debugging Facilities
13.4 Moving Up in the World: More Convenient DebuggingTools
13.5 Ensuring Consistency in Debugging Simulation Code
13.6 Syntax and Runtime Errors
13.7 Running GDB on R Itself
14: Performance Enhancement: Speed and Memory
14.1 Writing Fast R Code
14.2 The Dreaded for Loop
14.3 Functional Programming and Memory Issues
14.4 Using Rprof() to Find Slow Spots in Your Code
14.5 Byte Code Compilation
14.6 Oh No, the Data Doesn’t Fit into Memory!
15: Interfacing R to Other Languages
15.1 Writing C/C++ Functions to Be Called from R
15.2 Using R from Python
16: Parallel R
16.1 The Mutual Outlinks Problem
16.2 Introducing the snow Package
16.3 Resorting to C
16.4 General Performance Considerations
16.5 Debugging Parallel R Code
Appendix A: Installing R
A.1 Downloading R from CRAN
A.2 Installing from a Linux Package Manager
A.3 Installing from Source
Appendix B: Installing and Using Packages
B.1 Package Basics
B.2 Loading a Package from Your Hard Drive
B.3 Downloading a Package from the Web
B.4 Listing the Functions in a Package
Index
UPDATES
T H E A R T O F R PROGR A MMING 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 N O R M A N M A T L O F F T A M E Y O U R D A T A T A M E Y O U R D A T A R is the world’s most popular language for developing statistical software: Archaeologists use it to track the spread of ancient civilizations, drug companies use it to discover which medications are safe and effective, and actuaries use it to assess financial risks and keep markets running smoothly. • Interface R with C/C++ and Python for increased speed or functionality • Find new packages for text analysis, image manipula- tion, and thousands more • Squash annoying bugs with advanced debugging The Art of R Programming takes you on a guided tour of software development with R, from basic types and data structures to advanced topics like closures, recursion, and anonymous functions. No statistical knowledge is required, and your programming skills can range from hobbyist to pro. Along the way, you’ll learn about functional and object- oriented programming, running mathematical simulations, and rearranging complex data into simpler, more useful formats. You’ll also learn to: • Create artful graphs to visualize complex data sets and functions • Write more efficient code using parallel R and vectorization techniques Whether you’re designing aircraft, forecasting the weather, or you just need to tame your data, The Art of R Programming is your guide to harnessing the power of statistical computing. A B O U T T H E A U T H O R Norman Matloff is a professor of computer science (and a former professor of statistics) at the University of California, Davis. His research interests include parallel processing and statistical regression, and he is the author of several widely used web tutorials on software development. He has written articles for the New York Times, the Washington Post, Forbes Magazine, and the Los Angeles Times, and he is the co-author of The Art of Debugging (No Starch Press). TH E FI N EST I N G E E K E NTE RTAI N M E NT ™ www.nostarch.com “I LI E FLAT.” This book uses RepKover —a durable binding that won’t snap shut. FSC LOGO $39.95 ($41.95 CDN) S H E L V E I N : S T A T I S T I C A L S O F T W A R E C O M P U T E R S / M A T H E M A T I C A L & T H E A R T O F R P R O G R A M M I N G M A T L O F F THEART OF RPROGRAMMINGTHE ART OF R PROGRAMMING
THE ART OF R PROGRAMMING
THE ART OF R PROGRAMMING A Tour of Statistical Software Design by Norman Matloff San Francisco
THE ART OF R PROGRAMMING. Copyright © 2011 by Norman Matloff. 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 information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. 15 14 13 12 11 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-384-3 ISBN-13: 978-1-59327-384-2 Publisher: William Pollock Production Editor: Alison Law Cover and Interior Design: Octopod Studios Developmental Editor: Keith Fancher Technical Reviewer: Hadley Wickham Copyeditor: Marilyn Smith Compositors: Alison Law and Serena Yang Proofreader: Paula L. Fleming Indexer: BIM Indexing & Proofreading Services For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 38 Ringold Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com Library of Congress Cataloging-in-Publication Data 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. QA276.4.M2925 2011 519.50285'5133-dc23 R (Computer program language) I. Title. 2011025598 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 trademarked 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. 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 infor- mation contained in it.
B R I E F C O N T E N T S Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Chapter 3: Matrices and Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Chapter 4: Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Chapter 5: Data Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Chapter 6: Factors and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121 Chapter 7: R Programming Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 Chapter 8: Doing Math and Simulations in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189 Chapter 9: Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 Chapter 10: Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231 Chapter 11: String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Chapter 12: Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261 Chapter 13: Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 Chapter 14: Performance Enhancement: Speed and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . .305 Chapter 15: Interfacing R to Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323 Chapter 16: Parallel R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333 Appendix A: Installing R. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353 Appendix B: Installing and Using Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
分享到:
收藏