logo资料库

Advanced R programming-By Hadley Wickham.pdf

第1页 / 共454页
第2页 / 共454页
第3页 / 共454页
第4页 / 共454页
第5页 / 共454页
第6页 / 共454页
第7页 / 共454页
第8页 / 共454页
资料共454页,剩余部分请下载后查看
Introduction
Who should read this book
What you will get out of this book
Meta-techniques
Recommended reading
Getting help
Acknowledgements
Colophon
I Foundations
Data structures
Quiz
Vectors
Atomic vectors
Lists
Exercises
Attributes
Factors
Exercises
Matrices and arrays
Data frames
Creation
Testing and coercion
Combining data frames
Special columns
Subsetting
Data types
Atomic vectors
Lists
Matrices and arrays
Data frames
S3 objects
S4 objects
Exercises
Subsetting operators
Simplifying vs. preserving subsetting
$
Missing/out of bounds indices
Exercises
Subsetting and assignment
Applications
Lookup tables (character subsetting)
Matching and merging by hand (integer subsetting)
Random samples/bootstrap (integer subsetting)
Ordering (integer subsetting)
Expanding aggregated counts (integer subsetting)
Removing columns from data frame (character subsetting)
Selecting rows based on a condition (logical subsetting)
Boolean algebra vs sets (logical & integer subsetting)
Exercises
Vocabulary
The basics
Common data structures
Statistics
Working with R
I/O
Functions
Components of a function
Primitive functions
Exercises
Lexical scoping
Name masking
Functions vs. variables
A fresh start
Dynamic lookup
Exercises
Every operation is a function call
Function arguments
Calling functions
Calling a function given a list of arguments
Default and missing arguments
Lazy evaluation
...
Exercises
Special calls
Infix functions
Replacement functions
Exercises
Return values
on.exit()
Exercises
OO field guide
Introduction
Base types
S3
Recognising objects, generic functions and methods
Defining classes and creating objects
Creating new methods and generics
Method dispatch
Exercises
S4
Recognising objects, generic functions and methods
Defining classes and creating objects
Creating new methods and generics
Method dispatch
Exercises
RC
Defining classes and creating objects
Recognising objects and methods
Method dispatch
Exercises
Picking a system
Environments
Introduction
Environment basics
What is an environment?
Manipulating and inspecting environments
Special environments
Where
Exercises
Function environments
The environment where the function was created
The environment where the function lives
The environment created every time a function is run
The environment where the function was called
Exercises
Explicit scoping with local
Assignment: binding names to values
Regular binding
<<-
Delayed bindings
Active bindings
Exercises
Debugging, condition handling and defensive programming
Debugging techniques
Debugging tools
Determining the sequence of calls
Browsing on error
Browsing arbitrary code
The call stack: traceback(), where and recover().
Other types of failure
Condition handling
Ignore errors with try()
Handle conditions with tryCatch()
withCallingHandlers()
Custom signal classes
Exercises
Defensive programming
Exercises
II Functional programming
Functional programming
Other languages
Motivation
Anonymous functions
Exercises
Introduction to closures
Function factories
Mutable state
Exercises
Lists of functions
Moving lists of functions to the global environment
Exercises
Case study: numerical integration
Exercises
Functionals
Introduction
My first functional: lapply()
Looping patterns
Exercises
For loop functionals: friends of lapply()
Vector output: sapply and vapply
Multiple inputs: Map (and mapply)
Rolling computations
Parallelisation
Exercises
Data structure functionals
Matrix and array operations
Group apply
The plyr package
Exercises
Functional programming
Reduce()
Predicate functionals
Exercises
Mathematical functionals
Exercises
Converting loops to functionals, and when it's not possible
Modifying in place
Recursive relationships
While loops
A family of functions
Exercises
Function operators
In other languages
Behavioural FOs
Useful behavioural FOs
Memoisation
Capturing function invocations
Exercises
Output FOs
Minor modifications
Changing what a function does
Exercises
Input FOs
Prefilling function arguments: partial function application
Changing input types
Exercises
Combining FOs
Function composition
Logical predicates and boolean algebra
Exercises
The common pattern and a subtle bug
Exercises
III Computing on the language
Metaprogramming
Capturing expressions
Non-standard evaluation in subset
Exercises
Scoping issues
Exercises
Calling from another function
Substitute
Non-standard evaluation in substitute
Capturing unevaluated …
The downsides of non-standard evaluation
Applications
plyr::. and ggplot2::aes
Plyr: summarise, mutate and arrange
Conclusion
Expressions
Structure of expressions
Constants
Names
Calls
Exercises
Parsing and deparsing
Sourcing files from disk
Exercises
Capturing the current call
Other uses of call capturing
Exercises
Creating a function
Unenclose
Exercises
Walking the call tree with recursive functions
Finding F and T
Finding all variables created by assignment
Modifying the call tree
Exercises
Formulas
Formula as a quoting function
xtabs()
Formulas for modelling
Special contexts
Evaluate code in a special context
with_something
capture.output
Evaluating code in a new environment
Exercises
Anaphoric functions
With connection
Special environments for run-time checking
Logical abbreviations
Test that
Domain specific languages
HTML
Goal
Escaping
Basic tag functions
Tag functions
Processing all tags
Exercises
LaTeX
LaTeX mathematics
Goal
to_math
Known symbols
Unknown symbols
Known functions
Unknown functions
Exercises
IV Performance
Performance
Micro-benchmarking
Method dispatch
Extracting variables out of a data frame
Vectorised operations on a data frame
Brainstorming
Caching
Memoisation
Byte code compilation
Other people's code
Important vectorised functions
Rewrite in a lower-level language
Profiling and benchmarking
Performance profiling
Timing
Performance testing
Memory
object.size()
Exercises
Total memory use
Garbarge collection
Memory profiling with lineprof
Exercises
Modification in place
Loops
Exercises
High performance functions with Rcpp
Getting started
Getting started with C++
No inputs, scalar output
Scalar input, scalar output
Vector input, scalar output
Vector input, vector output
Matrix input, vector output
Using sourceCpp
Exercises
Rcpp classes and methods
Rcpp sugar
Arithmetic and logical operators
Logical summary functions
Vector views
Other useful functions
Missing values
Scalars
Strings
Boolean
Vectors
Exercises
The STL
Using iterators
Algorithms
Data structures
Vectors
Sets
Map
Exercises
Case studies
Gibbs sampler
R vectorisation vs. C++ vectorisation
Using Rcpp in a Package
Adding Rcpp to an existing package (Rcpp <= 0.10.6)
Adding Rcpp to an existing package (Rcpp >= 0.10.7)
More details
Learning more
More Rcpp
More C++
Acknowledgements
R's C interface
Introduction
Differences between R and C
Calling C functions from R
Basic data structures
Character vectors
Coercion and object creation
Object creation and garbage collection
Allocation shortcuts
Extracting C vectors
Modifying strings
Coercing scalars
Modifying objects
Pairlists and symbols
Missing and non-finite values
Checking types in C
Finding the C source code for a function
.External
Using C code in a package
V Packages
Package development philosophy
Getting started
Introduction to devtools
Package basics
Package essentials
Optional components
Getting started
The R/ directory
A minimal DESCRIPTION file
Other DESCRIPTION components
Source, binary and bundled packages
Package libraries
Installing packages
Exercises
The package development cycle
Dev mode
Key functions
Development cycles
Confirmatory programming
Exploratory programming
Package level documentation
README
plyr
stringr
NEWS
CITATION
Vignettes
Demos
Documenting functions
Help
Roxygen process
Common documentation tags
Documenting functions (and methods)
Documenting S3 generic functions and methods
Documenting S4 generic and methods
Documenting R5 methods
Documenting classes
Documenting a S3 class
Documenting an S4 class
Documenting an R5 class
Documenting datasets
Documenting packages
Text formatting
Lists
Tables
Mathematics
Character formatting
Dynamic help
Testing
Motivation
testthat test structure
Expectations
Tests
Contexts
Running tests
Testing files and directories
Autotest
R CMD check
Style guide
Notation and naming
File names
Identifiers
Syntax
Spacing
Curly braces
Line length
Indentation
Assignment
Organisation
Commenting guidelines
Namespaces
How do namespaces work
Exporting
Importing
Compiled code
How do they work
Git and github
Git
The basics
Github
Releasing a package
Checking
Version numbers
Publishing on CRAN
Publicising
Advanced R programming Author: Hadley Wickham
Contents 1 Introduction Who should read this book . . . . . . . . . . . . . . . . . . . . . . . . What you will get out of this book . . . . . . . . . . . . . . . . . . . . Meta-techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Recommended reading . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Colophon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I Foundations 2 Data structures Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Vectors Atomic vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Matrices and arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 17 19 19 20 21 21 22 22 24 25 26 26 27 28 30 31 32 34 34 37
CONTENTS Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing and coercion . . . . . . . . . . . . . . . . . . . . . . . . . Combining data frames . . . . . . . . . . . . . . . . . . . . . . . Special columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Subsetting Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Atomic vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Matrices and arrays . . . . . . . . . . . . . . . . . . . . . . . . . Data frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . S3 objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . S4 objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Subsetting operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . Simplifying vs. preserving subsetting . . . . . . . . . . . . . . . . $ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Missing/out of bounds indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises Subsetting and assignment . . . . . . . . . . . . . . . . . . . . . . . . . Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lookup tables (character subsetting) . . . . . . . . . . . . . . . . Matching and merging by hand (integer subsetting) . . . . . . . Random samples/bootstrap (integer subsetting) . . . . . . . . . . Ordering (integer subsetting) . . . . . . . . . . . . . . . . . . . . Expanding aggregated counts (integer subsetting) . . . . . . . . . Removing columns from data frame (character subsetting) . . . . Selecting rows based on a condition (logical subsetting) . . . . . Boolean algebra vs sets (logical & integer subsetting) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises 2 37 37 38 39 41 41 42 44 44 45 46 46 46 47 48 49 50 51 51 52 52 53 54 55 56 56 57 59 60
CONTENTS 4 Vocabulary The basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Common data structures . . . . . . . . . . . . . . . . . . . . . . . . . . Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Functions Components of a function . . . . . . . . . . . . . . . . . . . . . . . . . Primitive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises Lexical scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Name masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions vs. variables . . . . . . . . . . . . . . . . . . . . . . . . A fresh start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dynamic lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Every operation is a function call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function arguments Calling functions . . . . . . . . . . . . . . . . . . . . . . . . . . . Calling a function given a list of arguments . . . . . . . . . . . . Default and missing arguments . . . . . . . . . . . . . . . . . . . Lazy evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises Special calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Infix functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replacement functions . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . on.exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 61 61 63 64 64 65 67 68 68 69 69 70 72 73 73 75 75 77 77 79 79 80 83 84 84 85 86 88 88 91 91
CONTENTS 4 6 OO field guide Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Base types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . S3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Recognising objects, generic functions and methods . . . . . . . . Defining classes and creating objects . . . . . . . . . . . . . . . . Creating new methods and generics . . . . . . . . . . . . . . . . . Method dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises 92 92 93 94 94 96 98 99 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 S4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Recognising objects, generic functions and methods . . . . . . . . 103 Defining classes and creating objects . . . . . . . . . . . . . . . . 104 Creating new methods and generics . . . . . . . . . . . . . . . . . 106 Method dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 RC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Defining classes and creating objects . . . . . . . . . . . . . . . . 107 Recognising objects and methods . . . . . . . . . . . . . . . . . . 110 Method dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Picking a system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 7 Environments 112 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Environment basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 What is an environment? . . . . . . . . . . . . . . . . . . . . . . 112 Manipulating and inspecting environments . . . . . . . . . . . . . 113 Special environments . . . . . . . . . . . . . . . . . . . . . . . . . 116 Where . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Exercises Function environments . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
CONTENTS 5 The environment where the function was created . . . . . . . . . 120 The environment where the function lives . . . . . . . . . . . . . 121 The environment created every time a function is run . . . . . . 122 The environment where the function was called . . . . . . . . . . 123 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Exercises Explicit scoping with local . . . . . . . . . . . . . . . . . . . . . . . . 126 Assignment: binding names to values . . . . . . . . . . . . . . . . . . . 127 Regular binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 <<- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Delayed bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Active bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 8 Debugging, condition handling and defensive programming Debugging techniques Debugging tools 134 . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Determining the sequence of calls . . . . . . . . . . . . . . . . . . 138 Browsing on error . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Browsing arbitrary code . . . . . . . . . . . . . . . . . . . . . . . 141 The call stack: traceback(), where and recover(). . . . . . . . 142 Other types of failure . . . . . . . . . . . . . . . . . . . . . . . . 142 Condition handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Ignore errors with try() . . . . . . . . . . . . . . . . . . . . . . . 144 Handle conditions with tryCatch() . . . . . . . . . . . . . . . . 146 withCallingHandlers() . . . . . . . . . . . . . . . . . . . . . . 148 Custom signal classes . . . . . . . . . . . . . . . . . . . . . . . . . 149 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Exercises Defensive programming . . . . . . . . . . . . . . . . . . . . . . . . . . 151 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Exercises
CONTENTS II Functional programming 6 154 9 Functional programming Other languages 155 . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Anonymous functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Introduction to closures . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Function factories . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Mutable state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Exercises Lists of functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Moving lists of functions to the global environment . . . . . . . . 169 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Case study: numerical integration . . . . . . . . . . . . . . . . . . . . 170 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Exercises Exercises 10 Functionals 175 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 My first functional: lapply() . . . . . . . . . . . . . . . . . . . . . . . 176 Looping patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Exercises For loop functionals: friends of lapply() . . . . . . . . . . . . . . . . 181 Vector output: sapply and vapply . . . . . . . . . . . . . . . . . 182 Multiple inputs: Map (and mapply) . . . . . . . . . . . . . . . . . 184 Rolling computations . . . . . . . . . . . . . . . . . . . . . . . . . 186 Parallelisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Exercises Data structure functionals . . . . . . . . . . . . . . . . . . . . . . . . . 189 Matrix and array operations . . . . . . . . . . . . . . . . . . . . . 189 Group apply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 The plyr package . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
CONTENTS 7 Exercises Exercises Mathematical functionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Functional programming . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Reduce() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Predicate functionals . . . . . . . . . . . . . . . . . . . . . . . . . 195 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 197 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 . . . . . . 199 Modifying in place . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Recursive relationships . . . . . . . . . . . . . . . . . . . . . . . . 200 While loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 A family of functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Converting loops to functionals, and when it’s not possible Exercises 11 Function operators In other languages 208 . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Behavioural FOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 . . . . . . . . . . . . . . . . . . . . . . . 211 Useful behavioural FOs Memoisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Capturing function invocations . . . . . . . . . . . . . . . . . . . 215 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Output FOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Minor modifications . . . . . . . . . . . . . . . . . . . . . . . . . 218 Changing what a function does . . . . . . . . . . . . . . . . . . . 219 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Exercises Input FOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Prefilling function arguments: partial function application . . . . 221 Changing input types . . . . . . . . . . . . . . . . . . . . . . . . 224 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Exercises Combining FOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Function composition . . . . . . . . . . . . . . . . . . . . . . . . 226
分享到:
收藏