Home Page Icon
Home Page
Table of Contents for
Images
Close
Images
by Aimee Gott, Richard Pugh, Andy Nicholls
Sams Teach Yourself R in 24 Hours
About This E-Book
Title Page
Copyright Page
Contents at a Glance
Table of Contents
Preface
Who Should Read This Book?
What Should You Expect from This Book?
How Is This Book Organized?
About the Sample Code
Contacting the Authors
About the Authors
Dedications
Acknowledgments
We Want to Hear from You!
Reader Services
Hour 1. The R Community
A Concise History of R
The Birth of S
The Birth of R
The R Community
Mailing Lists
R Manuals
Online Resources
The R Consortium
User Events
R Development
Versions of R
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 2. The R Environment
Integrated Development Environments
The R GUI
The RStudio IDE
Other Development Environments
R Syntax
The Console
Scripting
R Objects
R Packages
The Search Path
Listing Objects
The R Workspace
Using R Packages
Finding the Right Package
Installing an R Package
Loading an R Package
Internal Help
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 3. Single-Mode Data Structures
The R Data Types
The mode Function
Vectors, Matrices, and Arrays
Vectors
Creating Vectors
Vector Attributes
Subscripting Vectors
Matrices
Creating Matrices
Matrix Attributes
Subscripting Matrices
Subscripting Matrices: Blanks, Positives, and Negatives
Dropping Dimensions
Subscripting Matrices: Logical Values
Subscripting Matrices: Character Values
Arrays
Creating Arrays
Array Attributes
Subscripting Arrays
Relationship Between Single-Mode Data Objects
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 4. Multi-Mode Data Structures
Multi-Mode Structures
Lists
What Is a List?
Creating an Empty List
Creating a Non-Empty List
Creating a List with Element Names
Creating a List: A Summary
List Attributes
Subscripting Lists
Subsetting the List
Reference List Elements
Adding List Elements
A Summary of List Syntax
Motivation for Lists
Value
Data Frames
Creating a Data Frame
Querying Data Frame Attributes
Selecting Columns from the Data Frame
Selecting Columns from the Data Frame
Subscripting Columns
Referencing as a Matrix
Summary of Subscripting Data Frames
Exploring Your Data
The Top and Bottom of Your Data
Viewing Your Data
Summarizing Your Data
Visualizing Your Data
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 5. Dates, Times, and Factors
Working with Dates and Times
Creating Date Objects
Creating Objects That Include Times
Manipulating Dates and Times
The lubridate Package
Working with Categorical Data
Creating Factors
Manipulating Factor Levels
Creating Factors from Continuous Data
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 6. Common R Utility Functions
Using R Functions
Functions for Numeric Data
Mathematical Functions and Operators
Statistical Summary Functions
Simulation and Statistical Distributions
Logical Data
Missing Data
Character Data
Simple Character Manipulation
Searching and Replacing
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 7. Writing Functions: Part I
The Motivation for Functions
A Closer Look at an R Function
Creating a Simple Function
Naming a Function
Defining Function Arguments
Function Scoping Rules
Return Objects
The If/Else Structure
A Simple R Example
Nested Statements
Using One Condition
Multiple Test Values
Summarizing to a Single Logical
Switching with Logical Input
Reversing Logical Values
Mixing Conditions
Control And/Or Statements
Returning Early
A Worked Example
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 8. Writing Functions: Part II
Errors and Warnings
Error Messages
Warning Messages
Checking Inputs
The Ellipsis
Using the Ellipsis
Passing Graphical Parameters Using the Ellipsis
Checking Multivalue Inputs
Using Input Definition
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 9. Loops and Summaries
Repetitive Tasks
What Is a Loop?
The for Function
The while Function
The “apply” Family of Functions
The Set of “apply” Functions
The apply Function
The “Margin”
A Simple apply Example
Using Multiple Margins
Using apply with Higher Dimension Structures
Passing Extra Arguments to the “applied” Function
Using apply with Our Own Functions
Passing Extra Arguments to Our Functions
Applying to Data Frames
The lapply Function
The split Function
Splitting Data Frames
Using lapply with Vectors
The Order of “apply” Inputs
Using lapply with Data Frames
The sapply Function
Returns from sapply
Why Not Just Stick with sapply?
The tapply Function
Multiple Grouping Variables
Multiple Returns
Return Values from tapply
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 10. Importing and Exporting
Working with Text Files
Reading in Text Files
Reading in CSV Files
Exporting Text Files
Faster Imports and Exports
Efficient Data Storage
Proprietary and Other Formats
Relational Databases
RODBC
DBI
Working with Microsoft Excel
Connecting to R from Excel
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 11. Data Manipulation and Transformation
Sorting
Sorting Data Frames
Descending Sorts
Appending
Merging
A Merge Example
Missing Data
Duplicate Values
Restructuring
Restructuring with reshape
Melting
Casting
Restructuring with tidyr
Data Aggregation
Using a “for” Loop
Using an “apply” Function
The aggregate Function
Using aggregate with a Formula
Using aggregate by Specifying Columns
Calculating Differences from Baseline
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 12. Efficient Data Handling in R
dplyr: A New Way of Handling Data
Creating a dplyr (tbl_df) Object
Sorting
Subscripting
Adding New Columns
Merging
Aggregation
The Pipe Operator
Efficient Data Handling with data.table
Creating a data.table
Setting a Key
Subscripting
Adding New Columns and Rows
Merging
Aggregation
Too Large for data.table
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 13. Graphics
Graphics Devices and Colors
Devices
Colors
High-Level Graphics Functions
Univariate Graphics
The plot Function
Aesthetics
Low-Level Graphics Functions
Points and Lines
Text
Legends
Other Low-Level Functions
Graphical Parameters
Controlling the Layout
Grid Layouts
The layout Function
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 14. The ggplot2 Package for Graphics
The Philosophy of ggplot2
Quick Plots and Basic Control
Using qplot
Titles and Axes
Working with Layers
Plots as Objects
Changing Plot Types
Plot Types
Combining Plot Types
Aesthetics
Control of Aesthetics
Scales and the Legend
Working with Grouped Data
Paneling (a.k.a Faceting)
Using facet_grid
Using facet_wrap
Faceting from qplot
Custom Plots
Working with ggplot
Coordinate Systems
Themes and Layout
Tweaking Individual Plots
Global Themes
Legend Layout
The ggvis Evolution
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 15. Lattice Graphics
The History of Trellis Graphics
The Lattice Package
Creating a Simple Lattice Graph
Lattice Graph Types
Plotting Subsets of Data
Graph Options
Titles and Axes
Plot Types and Formatting
Multiple Variables
Groups of Data
Using Panels
Controlling the Strip Headers
Multiple “By” Variables
Panel Functions
Controlling Styles
Previewing the Styles
Creating a Theme
Using a Theme
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 16. Introduction to R Models and Object Orientation
Statistical Models in R
Simple Linear Models
Fitting the Model
Assessing a Model in R
Model Summaries
Model Diagnostic Plots
Extracting Model Elements
Models as List Objects
Adding Model Lines to Plots
Making Model Predictions
Multiple Linear Regression
Updating Models
Comparing Nested Models
Interaction Terms
Assess Addition of Interaction Term
Factor Independent Variables
Including Factors
Variable Transformations
R and Object Orientation
Object Orientation
Linear Model Methods
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 17. Common R Models
Generalized Linear Models
GLM Definition
Fitting a GLM
Fitting Gaussian Models
The glm Object
Logistic Regression
Poisson Regression
GLM Extensions
Nonlinear Models
Nonlinear Regression
Nonlinear Model Extensions
Survival Analysis
The ovarian Data Frame
Censoring
Estimating the Survival Function
Proportional Hazards
Survival Model Extensions
Time Series Analysis
Time Series Objects
Decomposing Time Series
Smoothing
Autocorrelations
Fitting ARIMA Models
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 18. Code Efficiency
Determining Efficiency
Profiling Code
Benchmarking
Initialization
Vectorization
What Is Vectorization?
How Code Can Be Vectorized
Using Alternative Functions
Managing Memory Usage
Integrating with C++
When to Think about C++ and Rcpp
A Basic Function
Using R Functions in C++
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 19. Package Building
Why Build an R Package?
The Structure of an R Package
Creating the Package Structure
The DESCRIPTION File
The NAMESPACE File
The R Directory
The man Directory
Code Quality
Automated Documentation with roxygen2
Function Headers
Documenting the Package
Creating and Updating the Help Pages
Building a Package with devtools
Checking
Building
Installing
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 20. Advanced Package Building
Extending R Packages
Developing a Test Framework
An Introduction to testthat
Incorporating Tests into a Package
Including Data in Packages
Including a User Guide
Including a Vignette in a Package
Writing a Vignette
Code Using Rcpp
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 21. Writing R Classes
What Is a Class?
Object Orientation in R
Why Bother with Object Orientation?
Why Use S3?
Creating a New S3 Class
A More Formal Approach to Creating Classes
Generic Functions and Methods
Defining Methods for Arithmetic Operators
Lists vs. Attributes
Creating New Generics
Inheritance in S3
Documenting S3
Limitations of S3
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 22. Formal Class Systems
S4
Working with S4 Classes
Defining an S4 Class
Methods
Defining New Generics
Multiple Dispatch
Inheritance
Documenting S4
Reference Classes
Creating a New Reference Class
Defining Methods
Copying Reference Class Objects
Documenting Reference Classes
R6 Classes
Public and Private Members
An R6 Example
Other Class Systems
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 23. Dynamic Reporting
What Is Dynamic Reporting?
An Introduction to knitr
Simple Reports with RMarkdown
A Basic RMarkdown Document
Building an HTML File
Including R Code and Output
Reporting with LaTeX
A Basic LaTeX Document
Including Code in a LaTeX Document
Summary
Q&A
Workshop
Quiz
Answers
Activities
Hour 24. Building Web Applications with Shiny
A Simple Shiny Application
Structure of a Shiny Application
The ui Component
The server Component
Reactive Functions
Why Do We Need Reactive Functions?
Creating a Simple Reactive Function
Interactive Documents
Sharing Shiny Applications
Summary
Q&A
Workshop
Quiz
Answers
Activities
Appendix: Installation
Installing R
Installing R on Windows
Installing R on Mac OS X
Installing R on Linux
Installing Rtools for Windows
Installing the RStudio IDE
Index
Code Snippets
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Images
Next
Next Chapter
Images
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset