Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
R Cookbook
Preface
The Recipes
A Note on Terminology
Software and Platform Notes
Other Resources
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
1. Getting Started and Getting Help
Introduction
1.1. Downloading and Installing R
Problem
Solution
Discussion
See Also
1.2. Starting R
Problem
Solution
Discussion
Starting on Windows
Starting on OS X
Starting on Linux and Unix
See Also
1.3. Entering Commands
Problem
Solution
Discussion
See Also
1.4. Exiting from R
Problem
Solution
Discussion
See Also
1.5. Interrupting R
Problem
Solution
Discussion
See Also
1.6. Viewing the Supplied Documentation
Problem
Solution
Discussion
See Also
1.7. Getting Help on a Function
Problem
Solution
Discussion
See Also
1.8. Searching the Supplied Documentation
Problem
Solution
Discussion
See Also
1.9. Getting Help on a Package
Problem
Solution
Discussion
See Also
1.10. Searching the Web for Help
Problem
Solution
Discussion
See Also
1.11. Finding Relevant Functions and Packages
Problem
Solution
Discussion
See Also
1.12. Searching the Mailing Lists
Problem
Solution
Discussion
See Also
1.13. Submitting Questions to the Mailing Lists
Problem
Solution
Discussion
See Also
2. Some Basics
Introduction
2.1. Printing Something
Problem
Solution
Discussion
See Also
2.2. Setting Variables
Problem
Solution
Discussion
See Also
2.3. Listing Variables
Problem
Solution
Discussion
See Also
2.4. Deleting Variables
Problem
Solution
Discussion
See Also
2.5. Creating a Vector
Problem
Solution
Discussion
See Also
2.6. Computing Basic Statistics
Problem
Solution
Discussion
See Also
2.7. Creating Sequences
Problem
Solution
Discussion
See Also
2.8. Comparing Vectors
Problem
Solution
Discussion
See Also
2.9. Selecting Vector Elements
Problem
Solution
Discussion
See Also
2.10. Performing Vector Arithmetic
Problem
Solution
Discussion
See Also
2.11. Getting Operator Precedence Right
Problem
Solution
Discussion
See Also
2.12. Defining a Function
Problem
Solution
Discussion
See Also
2.13. Typing Less and Accomplishing More
Problem
Solution
Discussion
2.14. Avoiding Some Common Mistakes
Problem
Discussion
See Also
3. Navigating the Software
Introduction
3.1. Getting and Setting the Working Directory
Problem
Solution
Discussion
See Also
3.2. Saving Your Workspace
Problem
Solution
Discussion
See Also
3.3. Viewing Your Command History
Problem
Solution
Discussion
3.4. Saving the Result of the Previous Command
Problem
Solution
Discussion
See Also
3.5. Displaying the Search Path
Problem
Solution
Discussion
See Also
3.6. Accessing the Functions in a Package
Problem
Solution
Discussion
See Also
3.7. Accessing Built-in Datasets
Problem
Solution
Discussion
See Also
3.8. Viewing the List of Installed Packages
Problem
Solution
Discussion
See Also
3.9. Installing Packages from CRAN
Problem
Solution
Discussion
See Also
3.10. Setting a Default CRAN Mirror
Problem
Solution
Discussion
See Also
3.11. Suppressing the Startup Message
Problem
Solution
Discussion
See Also
3.12. Running a Script
Problem
Solution
Discussion
See Also
3.13. Running a Batch Script
Problem
Solution
Discussion
See Also
3.14. Getting and Setting Environment Variables
Problem
Solution
Discussion
3.15. Locating the R Home Directory
Problem
Solution
Discussion
3.16. Customizing R
Problem
Solution
Discussion
Setting Options
Loading Packages
Startup Sequence
See Also
4. Input and Output
Introduction
A Philosophical Note
4.1. Entering Data from the Keyboard
Problem
Solution
Discussion
See Also
4.2. Printing Fewer Digits (or More Digits)
Problem
Solution
Discussion
See Also
4.3. Redirecting Output to a File
Problem
Solution
Discussion
4.4. Listing Files
Problem
Solution
Discussion
See Also
4.5. Dealing with “Cannot Open File” in Windows
Problem
Solution
Discussion
4.6. Reading Fixed-Width Records
Problem
Solution
Discussion
See Also
4.7. Reading Tabular Data Files
Problem
Solution
Discussion
See Also
4.8. Reading from CSV Files
Problem
Solution
Discussion
See Also
4.9. Writing to CSV Files
Problem
Solution
Discussion
See Also
4.10. Reading Tabular or CSV Data from the Web
Problem
Solution
Discussion
See Also
4.11. Reading Data from HTML Tables
Problem
Solution
Discussion
See Also
4.12. Reading Files with a Complex Structure
Problem
Solution
Discussion
An Example
4.13. Reading from MySQL Databases
Problem
Solution
Discussion
See Also
4.14. Saving and Transporting Objects
Problem
Solution
Discussion
5. Data Structures
Introduction
Vectors
Lists
Mode: Physical Type
Class: Abstract Type
Scalars
Matrices
Arrays
Factors
Data Frames
5.1. Appending Data to a Vector
Problem
Solution
Discussion
5.2. Inserting Data into a Vector
Problem
Solution
Discussion
5.3. Understanding the Recycling Rule
Problem
Discussion
5.4. Creating a Factor (Categorical Variable)
Problem
Solution
Discussion
See Also
5.5. Combining Multiple Vectors into One Vector and a Factor
Problem
Solution
Discussion
5.6. Creating a List
Problem
Solution
Discussion
See Also
5.7. Selecting List Elements by Position
Problem
Solution
Discussion
5.8. Selecting List Elements by Name
Problem
Solution
Discussion
See Also
5.9. Building a Name/Value Association List
Problem
Solution
Discussion
5.10. Removing an Element from a List
Problem
Solution
Discussion
5.11. Flatten a List into a Vector
Problem
Solution
Discussion
See Also
5.12. Removing NULL Elements from a List
Problem
Solution
Discussion
See Also
5.13. Removing List Elements Using a Condition
Problem
Solution
Discussion
See Also
5.14. Initializing a Matrix
Problem
Solution
Discussion
See Also
5.15. Performing Matrix Operations
Problem
Solution
Discussion
5.16. Giving Descriptive Names to the Rows and Columns of a Matrix
Problem
Solution
Discussion
5.17. Selecting One Row or Column from a Matrix
Problem
Solution
Discussion
5.18. Initializing a Data Frame from Column Data
Problem
Solution
Discussion
5.19. Initializing a Data Frame from Row Data
Problem
Solution
Discussion
See Also
5.20. Appending Rows to a Data Frame
Problem
Solution
Discussion
5.21. Preallocating a Data Frame
Problem
Solution
Discussion
5.22. Selecting Data Frame Columns by Position
Problem
Solution
Discussion
See Also
5.23. Selecting Data Frame Columns by Name
Problem
Solution
Discussion
See Also
5.24. Selecting Rows and Columns More Easily
Problem
Solution
Discussion
5.25. Changing the Names of Data Frame Columns
Problem
Solution
Discussion
See Also
5.26. Editing a Data Frame
Problem
Solution
Discussion
See Also
5.27. Removing NAs from a Data Frame
Problem
Solution
Discussion
5.28. Excluding Columns by Name
Problem
Solution
Discussion
See Also
5.29. Combining Two Data Frames
Problem
Solution
Discussion
See Also
5.30. Merging Data Frames by Common Column
Problem
Solution
Discussion
See Also
5.31. Accessing Data Frame Contents More Easily
Problem
Solution
Discussion
5.32. Converting One Atomic Value into Another
Problem
Solution
Discussion
5.33. Converting One Structured Data Type into Another
Problem
Solution
Discussion
Problems with matrices
See Also
6. Data Transformations
Introduction
Defining Groups Via a Factor
6.1. Splitting a Vector into Groups
Problem
Solution
Discussion
See Also
6.2. Applying a Function to Each List Element
Problem
Solution
Discussion
See Also
6.3. Applying a Function to Every Row
Problem
Solution
Discussion
6.4. Applying a Function to Every Column
Problem
Solution
Discussion
See Also
6.5. Applying a Function to Groups of Data
Problem
Solution
Discussion
See Also
6.6. Applying a Function to Groups of Rows
Problem
Solution
Discussion
See Also
6.7. Applying a Function to Parallel Vectors or Lists
Problem
Solution
Discussion
7. Strings and Dates
Introduction
Classes for Dates and Times
See Also
7.1. Getting the Length of a String
Problem
Solution
Discussion
7.2. Concatenating Strings
Problem
Solution
Discussion
7.3. Extracting Substrings
Problem
Solution
Discussion
7.4. Splitting a String According to a Delimiter
Problem
Solution
Discussion
See Also
7.5. Replacing Substrings
Problem
Solution
Discussion
See Also
7.6. Seeing the Special Characters in a String
Problem
Solution
Discussion
7.7. Generating All Pairwise Combinations of Strings
Problem
Solution
Discussion
See Also
7.8. Getting the Current Date
Problem
Solution
Discussion
See Also
7.9. Converting a String into a Date
Problem
Solution
Discussion
7.10. Converting a Date into a String
Problem
Solution
Discussion
7.11. Converting Year, Month, and Day into a Date
Problem
Solution
Discussion
7.12. Getting the Julian Date
Problem
Solution
Discussion
7.13. Extracting the Parts of a Date
Problem
Solution
Discussion
7.14. Creating a Sequence of Dates
Problem
Solution
Discussion
8. Probability
Introduction
Names of Distributions
Getting Help on Probability Distributions
See Also
8.1. Counting the Number of Combinations
Problem
Solution
Discussion
See Also
8.2. Generating Combinations
Problem
Solution
Discussion
See Also
8.3. Generating Random Numbers
Problem
Solution
Discussion
See Also
8.4. Generating Reproducible Random Numbers
Problem
Solution
Discussion
See Also
8.5. Generating a Random Sample
Problem
Solution
Discussion
See Also
8.6. Generating Random Sequences
Problem
Solution
Discussion
8.7. Randomly Permuting a Vector
Problem
Solution
Discussion
See Also
8.8. Calculating Probabilities for Discrete Distributions
Problem
Solution
Discussion
See Also
8.9. Calculating Probabilities for Continuous Distributions
Problem
Solution
Discussion
See Also
8.10. Converting Probabilities to Quantiles
Problem
Solution
Discussion
See Also
8.11. Plotting a Density Function
Problem
Solution
Discussion
9. General Statistics
Introduction
Null Hypotheses, Alternative Hypotheses, and p-Values
Confidence Intervals
See Also
9.1. Summarizing Your Data
Problem
Solution
Discussion
See Also
9.2. Calculating Relative Frequencies
Problem
Solution
Discussion
9.3. Tabulating Factors and Creating Contingency Tables
Problem
Solution
Discussion
See Also
9.4. Testing Categorical Variables for Independence
Problem
Solution
Discussion
See Also
9.5. Calculating Quantiles (and Quartiles) of a Dataset
Problem
Solution
Discussion
9.6. Inverting a Quantile
Problem
Solution
Discussion
See Also
9.7. Converting Data to Z-Scores
Problem
Solution
Discussion
9.8. Testing the Mean of a Sample (t Test)
Problem
Solution
Discussion
See Also
9.9. Forming a Confidence Interval for a Mean
Problem
Solution
Discussion
9.10. Forming a Confidence Interval for a Median
Problem
Solution
Discussion
See Also
9.11. Testing a Sample Proportion
Problem
Solution
Discussion
9.12. Forming a Confidence Interval for a Proportion
Problem
Solution
Discussion
See Also
9.13. Testing for Normality
Problem
Solution
Discussion
See Also
9.14. Testing for Runs
Problem
Solution
Discussion
See Also
9.15. Comparing the Means of Two Samples
Problem
Solution
Discussion
See Also
9.16. Comparing the Locations of Two Samples Nonparametrically
Problem
Solution
Discussion
See Also
9.17. Testing a Correlation for Significance
Problem
Solution
Discussion
See Also
9.18. Testing Groups for Equal Proportions
Problem
Solution
Discussion
See Also
9.19. Performing Pairwise Comparisons Between Group Means
Problem
Solution
Discussion
See Also
9.20. Testing Two Samples for the Same Distribution
Problem
Solution
Discussion
10. Graphics
Introduction
The Illustrations
Notes on Graphics Functions
The Generic plot Function
Graphics in Other Packages
10.1. Creating a Scatter Plot
Problem
Solution
Discussion
See Also
10.2. Adding a Title and Labels
Problem
Solution
Discussion
10.3. Adding a Grid
Problem
Solution
Discussion
10.4. Creating a Scatter Plot of Multiple Groups
Problem
Solution
Discussion
See Also
10.5. Adding a Legend
Problem
Solution
Discussion
10.6. Plotting the Regression Line of a Scatter Plot
Problem
Solution
Discussion
See Also
10.7. Plotting All Variables Against All Other Variables
Problem
Solution
Discussion
10.8. Creating One Scatter Plot for Each Factor Level
Problem
Solution
Discussion
10.9. Creating a Bar Chart
Problem
Solution
Discussion
See Also
10.10. Adding Confidence Intervals to a Bar Chart
Problem
Solution
Discussion
See Also
10.11. Coloring a Bar Chart
Problem
Solution
Discussion
See Also
10.12. Plotting a Line from x and y Points
Problem
Solution
Discussion
See Also
10.13. Changing the Type, Width, or Color of a Line
Problem
Solution
Discussion
See Also
10.14. Plotting Multiple Datasets
Problem
Solution
Discussion
See Also
10.15. Adding Vertical or Horizontal Lines
Problem
Solution
Discussion
See Also
10.16. Creating a Box Plot
Problem
Solution
Discussion
See Also
10.17. Creating One Box Plot for Each Factor Level
Problem
Solution
Discussion
See Also
10.18. Creating a Histogram
Problem
Solution
Discussion
See Also
10.19. Adding a Density Estimate to a Histogram
Problem
Solution
Discussion
See Also
10.20. Creating a Discrete Histogram
Problem
Solution
Discussion
See Also
10.21. Creating a Normal Quantile-Quantile (Q-Q) Plot
Problem
Solution
Discussion
See Also
10.22. Creating Other Quantile-Quantile Plots
Problem
Solution
Discussion
10.23. Plotting a Variable in Multiple Colors
Problem
Solution
Discussion
See Also
10.24. Graphing a Function
Problem
Solution
Discussion
See Also
10.25. Pausing Between Plots
Problem
Solution
Discussion
See Also
10.26. Displaying Several Figures on One Page
Problem
Solution
Discussion
See Also
10.27. Opening Additional Graphics Windows
Problem
Solution
Discussion
See Also
10.28. Writing Your Plot to a File
Problem
Solution
Discussion
See Also
10.29. Changing Graphical Parameters
Problem
Solution
Discussion
See Also
11. Linear Regression and ANOVA
Introduction
ANOVA
See Also
11.1. Performing Simple Linear Regression
Problem
Solution
Discussion
11.2. Performing Multiple Linear Regression
Problem
Solution
Discussion
See Also
11.3. Getting Regression Statistics
Problem
Solution
Discussion
See Also
11.4. Understanding the Regression Summary
Problem
Discussion
See Also
11.5. Performing Linear Regression Without an Intercept
Problem
Solution
Discussion
11.6. Performing Linear Regression with Interaction Terms
Problem
Solution
Discussion
See Also
11.7. Selecting the Best Regression Variables
Problem
Solution
Discussion
See Also
11.8. Regressing on a Subset of Your Data
Problem
Solution
Discussion
11.9. Using an Expression Inside a Regression Formula
Problem
Solution
Discussion
See Also
11.10. Regressing on a Polynomial
Problem
Solution
Discussion
See Also
11.11. Regressing on Transformed Data
Problem
Solution
Discussion
See Also
11.12. Finding the Best Power Transformation (Box–Cox Procedure)
Problem
Solution
Discussion
See Also
11.13. Forming Confidence Intervals for Regression Coefficients
Problem
Solution
Discussion
See Also
11.14. Plotting Regression Residuals
Problem
Solution
Discussion
See Also
11.15. Diagnosing a Linear Regression
Problem
Solution
Discussion
See Also
11.16. Identifying Influential Observations
Problem
Solution
Discussion
See Also
11.17. Testing Residuals for Autocorrelation (Durbin–Watson Test)
Problem
Solution
Discussion
See Also
11.18. Predicting New Values
Problem
Solution
Discussion
See Also
11.19. Forming Prediction Intervals
Problem
Solution
Discussion
11.20. Performing One-Way ANOVA
Problem
Solution
Discussion
See Also
11.21. Creating an Interaction Plot
Problem
Solution
Discussion
See Also
11.22. Finding Differences Between Means of Groups
Problem
Solution
Discussion
See Also
11.23. Performing Robust ANOVA (Kruskal–Wallis Test)
Problem
Solution
Discussion
11.24. Comparing Models by Using ANOVA
Problem
Solution
Discussion
12. Useful Tricks
Introduction
12.1. Peeking at Your Data
Problem
Solution
Discussion
See Also
12.2. Widen Your Output
Problem
Solution
Discussion
See Also
12.3. Printing the Result of an Assignment
Problem
Solution
Discussion
See Also
12.4. Summing Rows and Columns
Problem
Solution
Discussion
12.5. Printing Data in Columns
Problem
Solution
Discussion
12.6. Binning Your Data
Problem
Solution
Discussion
12.7. Finding the Position of a Particular Value
Problem
Solution
Discussion
See Also
12.8. Selecting Every nth Element of a Vector
Problem
Solution
Discussion
See Also
12.9. Finding Pairwise Minimums or Maximums
Problem
Solution
Discussion
See Also
12.10. Generating All Combinations of Several Factors
Problem
Solution
Discussion
See Also
12.11. Flatten a Data Frame
Problem
Solution
Discussion
See Also
12.12. Sorting a Data Frame
Problem
Solution
Discussion
12.13. Sorting by Two Columns
Problem
Solution
Discussion
See Also
12.14. Stripping Attributes from a Variable
Problem
Solution
Discussion
See Also
12.15. Revealing the Structure of an Object
Problem
Solution
Discussion
12.16. Timing Your Code
Problem
Solution
Discussion
12.17. Suppressing Warnings and Error Messages
Problem
Solution
Discussion
See Also
12.18. Taking Function Arguments from a List
Problem
Solution
Discussion
See Also
12.19. Defining Your Own Binary Operators
Problem
Solution
Discussion
See Also
13. Beyond Basic Numerics and Statistics
Introduction
13.1. Minimizing or Maximizing a Single-Parameter Function
Problem
Solution
Discussion
See Also
13.2. Minimizing or Maximizing a Multiparameter Function
Problem
Solution
Discussion
See Also
13.3. Calculating Eigenvalues and Eigenvectors
Problem
Solution
Discussion
13.4. Performing Principal Component Analysis
Problem
Solution
Discussion
See Also
13.5. Performing Simple Orthogonal Regression
Problem
Solution
Discussion
See Also
13.6. Finding Clusters in Your Data
Problem
Solution
Discussion
See Also
13.7. Predicting a Binary-Valued Variable (Logistic Regression)
Problem
Solution
Discussion
See Also
13.8. Bootstrapping a Statistic
Problem
Solution
Discussion
See Also
13.9. Factor Analysis
Problem
Solution
Discussion
See Also
14. Time Series Analysis
Introduction
Date Versus Datetime
See Also
14.1. Representing Time Series Data
Problem
Solution
Discussion
See Also
14.2. Plotting Time Series Data
Problem
Solution
Discussion
See Also
14.3. Extracting the Oldest or Newest Observations
Problem
Solution
Discussion
See Also
14.4. Subsetting a Time Series
Problem
Solution
Discussion
See Also
14.5. Merging Several Time Series
Problem
Solution
Discussion
14.6. Filling or Padding a Time Series
Problem
Solution
Discussion
14.7. Lagging a Time Series
Problem
Solution
Discussion
14.8. Computing Successive Differences
Problem
Solution
Discussion
14.9. Performing Calculations on Time Series
Problem
Solution
Discussion
See Also
14.10. Computing a Moving Average
Problem
Solution
Discussion
See Also
14.11. Applying a Function by Calendar Period
Problem
Solution
Discussion
14.12. Applying a Rolling Function
Problem
Solution
Discussion
14.13. Plotting the Autocorrelation Function
Problem
Solution
Discussion
14.14. Testing a Time Series for Autocorrelation
Problem
Solution
Discussion
See Also
14.15. Plotting the Partial Autocorrelation Function
Problem
Solution
Discussion
See Also
14.16. Finding Lagged Correlations Between Two Time Series
Problem
Solution
Discussion
14.17. Detrending a Time Series
Problem
Solution
Discussion
14.18. Fitting an ARIMA Model
Problem
Solution
Discussion
See Also
14.19. Removing Insignificant ARIMA Coefficients
Problem
Solution
Discussion
14.20. Running Diagnostics on an ARIMA Model
Problem
Solution
Discussion
14.21. Making Forecasts from an ARIMA Model
Problem
Solution
Discussion
14.22. Testing for Mean Reversion
Problem
Solution
Discussion
See Also
14.23. Smoothing a Time Series
Problem
Solution
Discussion
See Also
Index
About the Author
Colophon
← Prev
Back
Next →
← Prev
Back
Next →