r in a nutshell

636 1.3K 0
r in a nutshell

Đ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

www.it-ebooks.info www.it-ebooks.info R IN A NUTSHELL www.it-ebooks.info www.it-ebooks.info R IN A NUTSHELL Joseph Adler Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info R in a Nutshell by Joseph Adler Copyright © 2010 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 infor- mation, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Mike Loukides Production Editor: Sumita Mukherji Production Services: Newgen North America, Inc. Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: December 2009: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trade- marks 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. TM This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-80170-0 [M] 1260894633 www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Part I. R Basics 1. Getting and Installing R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 R Versions 3 Getting and Installing Interactive R Binaries 3 Windows 4 Mac OS X 5 Linux and Unix Systems 5 2. The R User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 The R Graphical User Interface 7 Windows 8 Mac OS X 8 Linux and Unix 8 The R Console 11 Command-Line Editing 13 Batch Mode 13 Using R Inside Microsoft Excel 14 Other Ways to Run R 15 3. A Short R Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Basic Operations in R 17 Functions 19 Variables 20 Introduction to Data Structures 22 v www.it-ebooks.info Objects and Classes 25 Models and Formulas 26 Charts and Graphics 28 Getting Help 32 4. R Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 An Overview of Packages 35 Listing Packages in Local Libraries 36 Loading Packages 38 Loading Packages on Windows and Linux 38 Loading Packages on Mac OS X 38 Exploring Package Repositories 39 Exploring Packages on the Web 40 Finding and Installing Packages Inside R 40 Custom Packages 43 Creating a Package Directory 43 Building the Package 45 Part II. The R Language 5. An Overview of the R Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Expressions 49 Objects 50 Symbols 50 Functions 50 Objects Are Copied in Assignment Statements 52 Everything in R Is an Object 52 Special Values 53 NA 53 Inf and -Inf 53 NaN 54 NULL 54 Coercion 54 The R Interpreter 55 Seeing How R Works 57 6. R Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Constants 61 Numeric Vectors 61 Character Vectors 62 Symbols 63 Operators 64 Order of Operations 65 Assignments 67 Expressions 67 vi | Table of Contents www.it-ebooks.info Separating Expressions 67 Parentheses 68 Curly Braces 68 Control Structures 69 Conditional Statements 69 Loops 70 Accessing Data Structures 72 Data Structure Operators 73 Indexing by Integer Vector 73 Indexing by Logical Vector 76 Indexing by Name 76 R Code Style Standards 77 7. R Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Primitive Object Types 79 Vectors 82 Lists 83 Other Objects 84 Matrices 84 Arrays 84 Factors 85 Data Frames 87 Formulas 88 Time Series 89 Shingles 91 Dates and Times 91 Connections 92 Attributes 92 Class 95 8. Symbols and Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Symbols 97 Working with Environments 98 The Global Environment 99 Environments and Functions 100 Working with the Call Stack 100 Evaluating Functions in Different Environments 101 Adding Objects to an Environment 103 Exceptions 104 Signaling Errors 104 Catching Errors 105 9. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 The Function Keyword 107 Arguments 107 Return Values 109 Table of Contents | vii www.it-ebooks.info Functions As Arguments 109 Anonymous Functions 110 Properties of Functions 111 Argument Order and Named Arguments 113 Side Effects 114 Changes to Other Environments 114 Input/Output 115 Graphics 115 10. Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Overview of Object-Oriented Programming in R 118 Key Ideas 118 Implementation Example 119 Object-Oriented Programming in R: S4 Classes 125 Defining Classes 125 New Objects 126 Accessing Slots 126 Working with Objects 127 Creating Coercion Methods 127 Methods 128 Managing Methods 129 Basic Classes 130 More Help 130 Old-School OOP in R: S3 131 S3 Classes 131 S3 Methods 132 Using S3 Classes in S4 Classes 133 Finding Hidden S3 Methods 133 11. High-Performance R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Use Built-in Math Functions 135 Use Environments for Lookup Tables 136 Use a Database to Query Large Data Sets 136 Preallocate Memory 137 Monitor How Much Memory You Are Using 137 Monitoring Memory Usage 137 Increasing Memory Limits 138 Cleaning Up Objects 138 Functions for Big Data Sets 139 Parallel Computation with R 139 High-Performance R Binaries 140 Revolution R 140 Building Your Own 141 viii | Table of Contents www.it-ebooks.info [...]... first section leaves off, describing the R language in detail • Part III, Working with Data, covers data processing in R: loading data into R, transforming data, summarizing data, and plotting data Summary statistics and charts are an important part of statistical analysis, but many laypeople don’t think of these things as statistical analysis So, I cover these topics without using too much math in. .. palettes for loading data, transforming data, plotting data, building models, or doing any interesting work with data Commercial applications like SAS, SPSS, and S-PLUS include UIs with much more functionality The R Graphical User Interface | 9 www.it-ebooks.info Several projects are aiming to build an easier to use GUI for R: Rcmdr The Rcmdr project is an R package that provides an alternative GUI for R You... documentation for your system for more information about how to install software Installing R from downloaded files If you’d like, you can manually download R and install it later Currently, there are precompiled R packages for several flavors of Linux, including Red Hat, Debian, Ubuntu, and SUSE Precompiled binaries are also available for Solaris On Red Hat–style systems, you can install these packages... the term R to refer to a few different things: • A computer language • The interpreter that executes code written in RA system for plotting computer graphics described using the R language • The Windows, Mac OS, or Linux application that includes the interpreter, graphics system, standard packages, and user interface This chapter contains a short description of the R user interface and the R console,...www.it-ebooks.info Part III Working with Data 12 Saving, Loading, and Editing Data 147 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 Database Connection Packages RODBC DBI... analyze baseball statistics, and look for Internet security threats I’ve learned a lot about data, and matured a lot as a data analyst R, too, has matured a great deal over the past 10 years R is used at the world’s largest technology companies (including Google, Microsoft, and Facebook), the largest pharmaceutical companies (including Johnson & Johnson, Merck, and Pfizer), and at hundreds of other... commands without starting R This makes it easier to automate analyses, as you can change a couple of variables and rerun an analysis For example, to load a set of commands from the file generate_graphs .R, you would use a command like this: % R CMD BATCH generate_graphs .R R would run the commands in the input file generate_graphs .R, generating an output file called generate_graphs.Rout with the results... includes a single installer that will install R plus all the other software you need to use RExcel If you already have R installed, you can install RExcel as a package from CRAN The following set of commands will download RExcel, configure the RCOM server, install RDCOM, and launch the RExcel installer: > > > > > > > > > install.packages("RExcelInstaller", "rcom", "rsproxy") # configure rcom library(rcom)... console, and describes how R varies on different platforms If you’ve never used an interactive language, this chapter will explain some basic things you will need to know in order to work with R We’ll take a quick look at the R graphical user interface (GUI) on each platform and then talk about the most important part: the R console The R Graphical User Interface Let’s get started by launching R and taking... version of R runs on both PowerPC- and Intel-based Mac systems running Mac OS X 10.4.4 (Tiger) and higher If you’re using an older operating system, or an older computer, you can find older versions on the website that may work better with your system You’ll find three different R installers for Mac OS X: a three-way universal binary for Mac OS X 10.5 (Leopard) and higher, a legacy universal binary . leaves off, describing the R language in detail. • Part III, Working with Data, covers data processing in R: loading data into R, transforming data, summarizing data, and plotting data. Summary. look for Internet security threats. I’ve learned a lot about data, and matured a lot as a data analyst. R, too, has matured a great deal over the past 10 years. R is used at the world’s largest technology. Handbook logo, and the O’Reilly logo are registered trade- marks 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,

Ngày đăng: 24/04/2014, 16:02

Mục lục

  • Preface

    • Why I Wrote This Book

    • When Should You Use R?

    • How This Book Is Organized

    • Conventions Used in This Book

    • How to Contact Us

    • Getting and Installing Interactive R Binaries

      • Windows

      • Linux and Unix Systems

        • Installation using package management systems

        • Installing R from downloaded files

        • The R Graphical User Interface

          • Windows

          • The R Console

            • Command-Line Editing

            • Using R Inside Microsoft Excel

            • Other Ways to Run R

            • Chapter 3. A Short R Tutorial

              • Basic Operations in R

              • Introduction to Data Structures

              • Chapter 4. R Packages

                • 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 Packages on the Web

                    • Finding and Installing Packages Inside R

                      • Windows and Linux GUIs

Tài liệu cùng người dùng

Tài liệu liên quan