O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

R Cookbook, 2nd Edition

Book Description

With more than 275 practical recipes, this expanded edition helps you perform data analysis with R quickly and efficiently. The R language provides everything you need to do statistical work, but its structure can be difficult to master. These task-oriented recipes make you productive with R immediately. Solutions range from basic tasks to input and output, general statistics, graphics, and linear regression.

Each recipe addresses a specific problem and includes a discussion that explains the solution and provides insight into how it works. If you’re a beginner, R Cookbook will help get you started. If you’re an intermediate user, this book will jog your memory and expand your horizons. You’ll get the job done faster and learn more about R in the process.

  • Create vectors, handle variables, and perform other basic functions
  • Simplify data input and output
  • Tackle data structures such as matrices, lists, factors, and data frames
  • Work with probability, probability distributions, and random variables
  • Calculate statistics and confidence intervals and perform statistical tests
  • Create a variety of graphic displays
  • Build statistical models with linear regressions and analysis of variance (ANOVA)
  • Explore advanced statistical techniques, such as finding clusters in your data

Table of Contents

  1. Welcome to the R Cookbook, 2nd Edition
    1. The Recipes
    2. A Note on Terminology
    3. Software and Platform Notes
    4. Other Resources
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. 1. Getting Started and Getting Help
    1. Downloading and Installing R
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Installing RStudio
      1. Problem
      2. Solution
      3. Discussion
    3. Starting RStudio
      1. Problem
      2. Solution
      3. Discussion
    4. Entering Commands
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Exiting from RStudio
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Interrupting R
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Viewing the Supplied Documentation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Getting Help on a Function
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Searching the Supplied Documentation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Getting Help on a Package
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Searching the Web for Help
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. Finding Relevant Functions and Packages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. Searching the Mailing Lists
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Submitting Questions to Stack Overflow or Elsewhere in the Community
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  3. 2. Some Basics
    1. Printing Something to the Screen
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Setting Variables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. Listing Variables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. Deleting Variables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Creating a Vector
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Computing Basic Statistics
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Creating Sequences
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Comparing Vectors
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Selecting Vector Elements
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Performing Vector Arithmetic
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Getting Operator Precedence Right
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. Typing Less and Accomplishing More
      1. Problem
      2. Solution
      3. Discussion
    13. Creating a Pipeline of Function Calls
      1. Problem
      2. Solution
      3. Discussion
    14. Avoiding Some Common Mistakes
      1. Problem
      2. Discussion
      3. See Also
  4. 3. Navigating the Software
    1. Getting and Setting the Working Directory
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Creating a New RStudio Project
      1. Problem
      2. Solution
      3. Discussion
    3. Saving Your Workspace
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. Viewing Your Command History
      1. Problem
      2. Solution
      3. Discussion
    5. Saving the Result of the Previous Command
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Displaying Loaded Packages via the Search Path
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Viewing the List of Installed Packages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Accessing the Functions in a Package
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Accessing Built-in Datasets
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Installing Packages from CRAN
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Installing a Package from GitHub
      1. Problem
      2. Solution
      3. Discussion
    12. Setting or Changing a Default CRAN Mirror
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. Running a Script
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Running a Batch Script
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. Locating the R Home Directory
      1. Problem
      2. Solution
      3. Discussion
    16. Customizing R Startup
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Using R and RStudio in the Cloud
      1. Problem
      2. Solution
      3. Discussion
  5. 4. Input and Output
    1. Entering Data from the Keyboard
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Printing Fewer Digits (or More Digits)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. Redirecting Output to a File
      1. Problem
      2. Solution
      3. Discussion
    4. Listing Files
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Dealing with “Cannot Open File” in Windows
      1. Problem
      2. Solution
      3. Discussion
    6. Reading Fixed-Width Records
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Reading Tabular Data Files
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Reading from CSV Files
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Writing to CSV Files
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Reading Tabular or CSV Data from the Web
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Reading Data from Excel
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. Writing a Data Frame to Excel
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. Reading Data from a SAS File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Reading Data from HTML Tables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. Reading Files with a Complex Structure
      1. Problem
      2. Solution
      3. Discussion
      4. An Example
    16. Reading from MySQL Databases
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Accessing a Database with dbplyr
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. Saving and Transporting Objects
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  6. 5. Data Structures
    1. Vectors
    2. Lists
    3. Mode: Physical Type
    4. Class: Abstract Type
    5. Scalars
    6. Matrices
    7. Arrays
    8. Factors
    9. Data Frames
    10. Tibbles
    11. Appending Data to a Vector
      1. Problem
      2. Solution
      3. Discussion
    12. Inserting Data into a Vector
      1. Problem
      2. Solution
      3. Discussion
    13. Understanding the Recycling Rule
      1. Problem
      2. Discussion
    14. Creating a Factor (Categorical Variable)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. Combining Multiple Vectors into One Vector and a Factor
      1. Problem
      2. Solution
      3. Discussion
    16. Creating a List
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Selecting List Elements by Position
      1. Problem
      2. Solution
      3. Discussion
    18. Selecting List Elements by Name
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. Building a Name/Value Association List
      1. Problem
      2. Solution
      3. Discussion
    20. Removing an Element from a List
      1. Problem
      2. Solution
      3. Discussion
    21. Flattening a List into a Vector
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. Removing NULL Elements from a List
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    23. Removing List Elements Using a Condition
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    24. Initializing a Matrix
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    25. Performing Matrix Operations
      1. Problem
      2. Solution
      3. Discussion
    26. Giving Descriptive Names to the Rows and Columns of a Matrix
      1. Problem
      2. Solution
      3. Discussion
    27. Selecting One Row or Column from a Matrix
      1. Problem
      2. Solution
      3. Discussion
    28. Initializing a Data Frame from Column Data
      1. Problem
      2. Solution
      3. Discussion
    29. Initializing a Data Frame from Row Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    30. Appending Rows to a Data Frame
      1. Problem
      2. Solution
      3. Discussion
    31. Selecting Data Frame Columns by Position
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    32. Selecting Data Frame Columns by Name
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    33. Changing the Names of Data Frame Columns
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    34. Removing NAs from a Data Frame
      1. Problem
      2. Solution
      3. Discussion
    35. Excluding Columns by Name
      1. Problem
      2. Solution
      3. Discussion
    36. Combining Two Data Frames
      1. Problem
      2. Solution
      3. Discussion
    37. Merging Data Frames by Common Column
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    38. Converting One Atomic Value into Another
      1. Problem
      2. Solution
      3. Discussion
    39. Converting One Structured Data Type into Another
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  7. 6. Data Transformations
    1. Applying a Function to Each List Element
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Applying a Function to Every Row of a Data Frame
      1. Problem
      2. Solution
      3. Discussion
    3. Applying a Function to Every Row of a Matrix
      1. Problem
      2. Solution
      3. Discussion
    4. Applying a Function to Every Column
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Applying a Function to Parallel Vectors or Lists
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Applying a Function to Groups of Data
      1. Problem
      2. Solution
      3. Discussion
    7. Creating a New Column Based on Some Condition
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  8. 7. Strings and Dates
    1. Classes for Dates and Times
    2. See Also
    3. Getting the Length of a String
      1. Problem
      2. Solution
      3. Discussion
    4. Concatenating Strings
      1. Problem
      2. Solution
      3. Discussion
    5. Extracting Substrings
      1. Problem
      2. Solution
      3. Discussion
    6. Splitting a String According to a Delimiter
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Replacing Substrings
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Generating All Pairwise Combinations of Strings
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Getting the Current Date
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Converting a String into a Date
      1. Problem
      2. Solution
      3. Discussion
    11. Converting a Date into a String
      1. Problem
      2. Solution
      3. Discussion
    12. Converting Year, Month, and Day into a Date
      1. Problem
      2. Solution
      3. Discussion
    13. Getting the Julian Date
      1. Problem
      2. Solution
      3. Discussion
    14. Extracting the Parts of a Date
      1. Problem
      2. Solution
      3. Discussion
    15. Creating a Sequence of Dates
      1. Problem
      2. Solution
      3. Discussion
  9. 8. Probability
    1. Names of Distributions
    2. Getting Help on Probability Distributions
    3. See Also
    4. Counting the Number of Combinations
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Generating Combinations
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Generating Random Numbers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Generating Reproducible Random Numbers
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Generating a Random Sample
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Generating Random Sequences
      1. Problem
      2. Solution
      3. Discussion
    10. Randomly Permuting a Vector
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Calculating Probabilities for Discrete Distributions
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. Calculating Probabilities for Continuous Distributions
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. Converting Probabilities to Quantiles
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Plotting a Density Function
      1. Problem
      2. Solution
      3. Discussion
  10. 9. General Statistics
    1. Null Hypotheses, Alternative Hypotheses, and p-Values
    2. Confidence Intervals
    3. See Also
    4. Summarizing Your Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Calculating Relative Frequencies
      1. Problem
      2. Solution
      3. Discussion
    6. Tabulating Factors and Creating Contingency Tables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Testing Categorical Variables for Independence
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Calculating Quantiles (and Quartiles) of a Dataset
      1. Problem
      2. Solution
      3. Discussion
    9. Inverting a Quantile
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Converting Data to z-Scores
      1. Problem
      2. Solution
      3. Discussion
    11. Testing the Mean of a Sample (t-Test)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. Forming a Confidence Interval for a Mean
      1. Problem
      2. Solution
      3. Discussion
    13. Forming a Confidence Interval for a Median
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Testing a Sample Proportion
      1. Problem
      2. Solution
      3. Discussion
    15. Forming a Confidence Interval for a Proportion
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. Testing for Normality
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Testing for Runs
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. Comparing the Means of Two Samples
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. Comparing the Locations of Two Samples Nonparametrically
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    20. Testing a Correlation for Significance
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    21. Testing Groups for Equal Proportions
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. Performing Pairwise Comparisons Between Group Means
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    23. Testing Two Samples for the Same Distribution
      1. Problem
      2. Solution
      3. Discussion
  11. 10. Graphics
    1. The Illustrations
    2. Notes on ggplot2 Basics
    3. “Long” Versus “Wide” Data with ggplot
    4. Graphics in Other Packages
    5. Creating a Scatter Plot
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Adding a Title and Labels
      1. Problem
      2. Solution
      3. Discussion
    7. Adding (or Removing) a Grid
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Applying a Theme to a ggplot Figure
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Creating a Scatter Plot of Multiple Groups
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Adding (or Removing) a Legend
      1. Problem
      2. Solution
      3. Discussion
    11. Plotting the Regression Line of a Scatter Plot
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. Plotting All Variables Against All Other Variables
      1. Problem
      2. Solution
      3. Discussion
    13. Creating One Scatter Plot for Each Group
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Creating a Bar Chart
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. Adding Confidence Intervals to a Bar Chart
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. Coloring a Bar Chart
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Plotting a Line from x and y Points
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. Changing the Type, Width, or Color of a Line
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. Plotting Multiple Datasets
      1. Problem
      2. Solution
      3. Discussion
    20. Adding Vertical or Horizontal Lines
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    21. Creating a Boxplot
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. Creating One Boxplot for Each Factor Level
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    23. Creating a Histogram
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    24. Adding a Density Estimate to a Histogram
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    25. Creating a Normal Quantile–Quantile Plot
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    26. Creating Other Quantile–Quantile Plots
      1. Problem
      2. Solution
      3. Discussion
    27. Plotting a Variable in Multiple Colors
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    28. Graphing a Function
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    29. Displaying Several Figures on One Page
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    30. Writing Your Plot to a File
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  12. 11. Linear Regression and ANOVA
    1. ANOVA
    2. Example Data
    3. See Also
    4. Performing Simple Linear Regression
      1. Problem
      2. Solution
      3. Discussion
    5. Performing Multiple Linear Regression
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Getting Regression Statistics
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Understanding the Regression Summary
      1. Problem
      2. Discussion
      3. See Also
    8. Performing Linear Regression Without an Intercept
      1. Problem
      2. Solution
      3. Discussion
    9. Regressing Only Variables That Highly Correlate with Your Dependent Variable
      1. Problem
      2. Solution
      3. Discussion
    10. Performing Linear Regression with Interaction Terms
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Selecting the Best Regression Variables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    12. Regressing on a Subset of Your Data
      1. Problem
      2. Solution
      3. Discussion
    13. Using an Expression Inside a Regression Formula
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Regressing on a Polynomial
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. Regressing on Transformed Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. Finding the Best Power Transformation (Box–Cox Procedure)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Forming Confidence Intervals for Regression Coefficients
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. Plotting Regression Residuals
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. Diagnosing a Linear Regression
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    20. Identifying Influential Observations
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    21. Testing Residuals for Autocorrelation (Durbin–Watson Test)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. Predicting New Values
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    23. Forming Prediction Intervals
      1. Problem
      2. Solution
      3. Discussion
    24. Performing One-Way ANOVA
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    25. Creating an Interaction Plot
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    26. Finding Differences Between Means of Groups
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    27. Performing Robust ANOVA (Kruskal–Wallis Test)
      1. Problem
      2. Solution
      3. Discussion
    28. Comparing Models by Using ANOVA
      1. Problem
      2. Solution
      3. Discussion
  13. 12. Useful Tricks
    1. Peeking at Your Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Printing the Result of an Assignment
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. Summing Rows and Columns
      1. Problem
      2. Solution
      3. Discussion
    4. Printing Data in Columns
      1. Problem
      2. Solution
      3. Discussion
    5. Binning Your Data
      1. Problem
      2. Solution
      3. Discussion
    6. Finding the Position of a Particular Value
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Selecting Every nth Element of a Vector
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Finding Minimums or Maximums
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Generating All Combinations of Several Variables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Flattening a Data Frame
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Sorting a Data Frame
      1. Problem
      2. Solution
      3. Discussion
    12. Stripping Attributes from a Variable
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. Revealing the Structure of an Object
      1. Problem
      2. Solution
      3. Discussion
    14. Timing Your Code
      1. Problem
      2. Solution
      3. Discussion
    15. Suppressing Warnings and Error Messages
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. Taking Function Arguments from a List
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Defining Your Own Binary Operators
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. Suppressing the Startup Message
      1. Problem
      2. Solution
      3. Discussion
    19. Getting and Setting Environment Variables
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    20. Use Code Sections
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    21. Executing R in Parallel Locally
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. Executing R in Parallel Remotely
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  14. 13. Beyond Basic Numerics and Statistics
    1. Minimizing or Maximizing a Single-Parameter Function
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Minimizing or Maximizing a Multiparameter Function
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. Calculating Eigenvalues and Eigenvectors
      1. Problem
      2. Solution
      3. Discussion
    4. Performing Principal Component Analysis
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Performing Simple Orthogonal Regression
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Finding Clusters in Your Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Predicting a Binary-Valued Variable (Logistic Regression)
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Bootstrapping a Statistic
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Factor Analysis
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  15. 14. Time Series Analysis
    1. Other Representations
    2. Date Versus Datetime
    3. See Also
    4. Representing Time Series Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    5. Plotting Time Series Data
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Extracting the Oldest or Newest Observations
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Subsetting a Time Series
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Merging Several Time Series
      1. Problem
      2. Solution
      3. Discussion
    9. Filling or Padding a Time Series
      1. Problem
      2. Solution
      3. Discussion
    10. Lagging a Time Series
      1. Problem
      2. Solution
      3. Discussion
    11. Computing Successive Differences
      1. Problem
      2. Solution
      3. Discussion
    12. Performing Calculations on Time Series
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. Computing a Moving Average
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Applying a Function by Calendar Period
      1. Problem
      2. Solution
      3. Discussion
    15. Applying a Rolling Function
      1. Problem
      2. Solution
      3. Discussion
    16. Plotting the Autocorrelation Function
      1. Problem
      2. Solution
      3. Discussion
    17. Testing a Time Series for Autocorrelation
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    18. Plotting the Partial Autocorrelation Function
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    19. Finding Lagged Correlations Between Two Time Series
      1. Problem
      2. Solution
      3. Discussion
    20. Detrending a Time Series
      1. Problem
      2. Solution
      3. Discussion
    21. Fitting an ARIMA Model
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    22. Removing Insignificant ARIMA Coefficients
      1. Problem
      2. Solution
      3. Discussion
    23. Running Diagnostics on an ARIMA Model
      1. Problem
      2. Solution
      3. Discussion
    24. Making Forecasts from an ARIMA Model
      1. Problem
      2. Solution
      3. Discussion
    25. Plotting a Forecast
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    26. Testing for Mean Reversion
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    27. Smoothing a Time Series
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  16. 15. Simple Programming
    1. Choosing Between Two Alternatives: if/else
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    2. Iterating with a Loop
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. Defining a Function
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    4. Creating a Local Variable
      1. Problem
      2. Solution
      3. Discussion
    5. Choosing Between Multiple Alternatives: switch
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Defining Defaults for Function Parameters
      1. Problem
      2. Solution
      3. Discussion
    7. Signaling Errors
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Protecting Against Errors
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    9. Creating an Anonymous Function
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Creating a Collection of Reusable Functions
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    11. Automatically Reindenting Code
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
  17. 16. R Markdown and Publishing
    1. Creating a New Document
      1. Problem
      2. Solution
      3. Discussion
    2. Adding a Title, Author, or Date
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    3. Formatting Document Text
      1. Problem
      2. Solution
      3. See Also
    4. Inserting Document Headings
      1. Problem
      2. Solution
      3. Discussion
    5. Inserting a List
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    6. Showing Output from R Code
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    7. Controlling Which Code and Results Are Shown
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    8. Inserting a Plot
      1. Problem
      2. Solution
      3. Discussion
    9. Inserting a Table
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    10. Inserting a Table of Data
      1. Problem
      2. Solution
      3. Discussion
    11. Inserting Math Equations
      1. Problem
      2. Solution
      3. Discussion
    12. Generating HTML Output
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    13. Generating PDF Output
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    14. Generating Microsoft Word Output
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    15. Generating Presentation Output
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    16. Creating a Parameterized Report
      1. Problem
      2. Solution
      3. Discussion
      4. See Also
    17. Organizing Your R Markdown Workflow
      1. Problem
      2. Solution
      3. See Also
  18. Index