1. Trang chủ
  2. » Công Nghệ Thông Tin

R in a nutshell, 2nd edition

722 164 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 722
Dung lượng 14,16 MB

Nội dung

www.it-ebooks.info www.it-ebooks.info R IN A NUTSHELL Second Edition Joseph Adler Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo www.it-ebooks.info R in a Nutshell, Second Edition by Joseph Adler Copyright © 2012 Joseph Adler 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 corporate@oreilly.com Editors: Mike Loukides and Meghan Blanchette Production Editor: Holly Bauer Proofreader: Julie Van Keuren Indexer: Fred Brown Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Rebecca Demarest September 2009: October 2012: First Edition Second Edition Revision History for the Second Edition: 2012-09-25 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449312084 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc R in a Nutshell, the image of a harpy eagle, 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 trademark 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 author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-449-31208-4 [LSI] 1348585490 www.it-ebooks.info Table of Contents Preface xiii Part I R Basics Getting and Installing R R Versions Getting and Installing Interactive R Binaries Windows Mac OS X Linux and Unix Systems 3 5 The R User Interface The R Graphical User Interface Windows Mac OS X Linux and Unix The R Console Command-Line Editing Batch Mode Using R Inside Microsoft Excel RStudio Other Ways to Run R 8 11 13 13 14 15 17 A Short R Tutorial 19 Basic Operations in R Functions Variables 19 21 22 iii www.it-ebooks.info Introduction to Data Structures Objects and Classes Models and Formulas Charts and Graphics Getting Help 24 27 28 30 35 R Packages 37 An Overview of Packages Listing Packages in Local Libraries Loading Packages Loading Packages on Windows and Linux Loading Packages on Mac OS X Exploring Package Repositories Exploring R Package Repositories on the Web Finding and Installing Packages Inside R Installing Packages From Other Repositories Custom Packages Creating a Package Directory Building the Package 37 38 40 40 40 41 42 42 45 45 45 47 Part II The R Language An Overview of the R Language 51 Expressions Objects Symbols Functions Objects Are Copied in Assignment Statements Everything in R Is an Object Special Values NA Inf and -Inf NaN NULL Coercion The R Interpreter Seeing How R Works 51 52 52 52 54 55 55 55 56 56 56 56 57 59 R Syntax 63 Constants Numeric Vectors Character Vectors Symbols Operators Order of Operations 63 63 64 65 66 67 iv | Table of Contents www.it-ebooks.info Assignments Expressions Separating Expressions Parentheses Curly Braces Control Structures Conditional Statements Loops Accessing Data Structures Data Structure Operators Indexing by Integer Vector Indexing by Logical Vector Indexing by Name R Code Style Standards 69 69 69 70 70 71 71 72 75 75 76 78 79 80 R Objects 83 Primitive Object Types Vectors Lists Other Objects Matrices Arrays Factors Data Frames Formulas Time Series Shingles Dates and Times Connections Attributes Class 83 86 87 88 88 89 89 91 92 94 95 95 96 96 99 Symbols and Environments 101 Symbols Working with Environments The Global Environment Environments and Functions Working with the Call Stack Evaluating Functions in Different Environments Adding Objects to an Environment Exceptions Signaling Errors Catching Errors 101 102 103 104 104 105 107 108 108 109 Functions 111 The Function Keyword 111 Table of Contents | v www.it-ebooks.info Arguments Return Values Functions as Arguments Anonymous Functions Properties of Functions Argument Order and Named Arguments Side Effects Changes to Other Environments Input/Output Graphics 111 113 113 114 115 117 118 118 119 119 10 Object-Oriented Programming 121 Overview of Object-Oriented Programming in R Key Ideas Implementation Example Object-Oriented Programming in R: S4 Classes Defining Classes New Objects Accessing Slots Working with Objects Creating Coercion Methods Methods Managing Methods Basic Classes More Help Old-School OOP in R: S3 S3 Classes S3 Methods Using S3 Classes in S4 Classes Finding Hidden S3 Methods 122 122 123 129 129 130 130 131 131 132 133 134 135 135 135 136 137 137 Part III Working with Data 11 Saving, Loading, and Editing Data 141 Entering Data Within R Entering Data Using R Commands Using the Edit GUI Saving and Loading R Objects Saving Objects with save Importing Data from External Files Text Files Other Software Exporting Data Importing Data From Databases Export Then Import vi | Table of Contents www.it-ebooks.info 141 141 142 145 145 146 146 154 155 156 156 Database Connection Packages RODBC DBI TSDBI Getting Data from Hadoop 156 157 167 172 172 12 Preparing Data 173 Combining Data Sets Pasting Together Data Structures Merging Data by Common Fields Transformations Reassigning Variables The Transform Function Applying a Function to Each Element of an Object Binning Data Shingles Cut Combining Objects with a Grouping Variable Subsets Bracket Notation subset Function Random Sampling Summarizing Functions tapply, aggregate Aggregating Tables with rowsum Counting Values Reshaping Data Data Cleaning Finding and Removing Duplicates Sorting 173 174 177 179 179 179 180 185 185 186 187 187 188 188 189 190 190 193 194 196 205 205 206 Part IV Data Visualization 13 Graphics 213 An Overview of R Graphics Scatter Plots Plotting Time Series Bar Charts Pie Charts Plotting Categorical Data Three-Dimensional Data Plotting Distributions Box Plots Graphics Devices Customizing Charts 213 214 220 222 226 227 232 239 242 246 247 Table of Contents | vii www.it-ebooks.info Common Arguments to Chart Functions Graphical Parameters Basic Graphics Functions 247 247 257 14 Lattice Graphics 267 History An Overview of the Lattice Package How Lattice Works A Simple Example Using Lattice Functions Custom Panel Functions High-Level Lattice Plotting Functions Univariate Trellis Plots Bivariate Trellis Plots Trivariate Plots Other Plots Customizing Lattice Graphics Common Arguments to Lattice Functions trellis.skeleton Controlling How Axes Are Drawn Parameters plot.trellis strip.default simpleKey Low-Level Functions Low-Level Graphics Functions Panel Functions 267 268 268 268 270 272 272 273 297 305 310 312 312 313 314 315 319 320 321 322 322 323 15 ggplot2 325 A Short Introduction The Grammar of Graphics A More Complex Example: Medicare Data Quick Plot Creating Graphics with ggplot2 Learning More 325 328 333 342 343 347 Part V Statistics with R 16 Analyzing Data 351 Summary Statistics Correlation and Covariance Principal Components Analysis Factor Analysis Bootstrap Resampling viii | Table of Contents www.it-ebooks.info 351 354 357 360 361 ... leaves off, describing the R language in detail • Part III, Working with Data, covers data processing in R: loading data into R, transforming data, and summarizing data • Part IV, Data Visualization,... of Packages Listing Packages in Local Libraries Loading Packages Loading Packages on Windows and Linux Loading Packages on Mac OS X Exploring Package Repositories Exploring R Package Repositories... Notation subset Function Random Sampling Summarizing Functions tapply, aggregate Aggregating Tables with rowsum Counting Values Reshaping Data Data Cleaning Finding and Removing Duplicates Sorting

Ngày đăng: 19/04/2019, 16:44