R Cookbook: Proven Recipes for Data Analysis, Statistics, and Graphics

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Perform data analysis with R quickly and efficiently with more than 275 practical recipes in this expanded second edition. 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 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

Author(s): J. D. Long, Paul Teetor
Edition: 2
Publisher: O'Reilly Media
Year: 2019

Language: English
Commentary: Vector PDF
Pages: 600
City: Sebastopol, CA
Tags: Data Analysis; Programming; Clustering; Data Visualization; Cookbook; R; Statistics; Best Practices; Linear Regression; Logistic Regression; Probability Theory; Time Series Analysis

Cover
Copyright
Table of Contents
Welcome to the R Cookbook, 2nd Edition
The Recipes
A Note on Terminology
Software and Platform Notes
Other Resources
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Getting Started and Getting Help
1.1 Downloading and Installing R
Problem
Solution
Discussion
See Also
1.2 Installing RStudio
Problem
Solution
Discussion
1.3 Starting RStudio
Problem
Solution
Discussion
1.4 Entering Commands
Problem
Solution
Discussion
See Also
1.5 Exiting from RStudio
Problem
Solution
Discussion
See Also
1.6 Interrupting R
Problem
Solution
Discussion
See Also
1.7 Viewing the Supplied Documentation
Problem
Solution
Discussion
See Also
1.8 Getting Help on a Function
Problem
Solution
Discussion
See Also
1.9 Searching the Supplied Documentation
Problem
Solution
Discussion
See Also
1.10 Getting Help on a Package
Problem
Solution
Discussion
See Also
1.11 Searching the Web for Help
Problem
Solution
Discussion
See Also
1.12 Finding Relevant Functions and Packages
Problem
Solution
Discussion
See Also
1.13 Searching the Mailing Lists
Problem
Solution
Discussion
See Also
1.14 Submitting Questions to Stack Overflow or Elsewhere in the Community
Problem
Solution
Discussion
See Also
Chapter 2. Some Basics
2.1 Printing Something to the Screen
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 Typing Less and Accomplishing More
Problem
Solution
Discussion
2.13 Creating a Pipeline of Function Calls
Problem
Solution
Discussion
2.14 Avoiding Some Common Mistakes
Problem
Discussion
See Also
Chapter 3. Navigating the Software
3.1 Getting and Setting the Working Directory
Problem
Solution
Discussion
See Also
3.2 Creating a New RStudio Project
Problem
Solution
Discussion
3.3 Saving Your Workspace
Problem
Solution
Discussion
See Also
3.4 Viewing Your Command History
Problem
Solution
Discussion
3.5 Saving the Result of the Previous Command
Problem
Solution
Discussion
See Also
3.6 Displaying Loaded Packages via the Search Path
Problem
Solution
Discussion
See Also
3.7 Viewing the List of Installed Packages
Problem
Solution
Discussion
See Also
3.8 Accessing the Functions in a Package
Problem
Solution
Discussion
See Also
3.9 Accessing Built-in Datasets
Problem
Solution
Discussion
See Also
3.10 Installing Packages from CRAN
Problem
Solution
Discussion
See Also
3.11 Installing a Package from GitHub
Problem
Solution
Discussion
3.12 Setting or Changing a Default CRAN Mirror
Problem
Solution
Discussion
See Also
3.13 Running a Script
Problem
Solution
Discussion
See Also
3.14 Running a Batch Script
Problem
Solution
Discussion
See Also
3.15 Locating the R Home Directory
Problem
Solution
Discussion
3.16 Customizing R Startup
Problem
Solution
Discussion
See Also
3.17 Using R and RStudio in the Cloud
Problem
Solution
Discussion
Chapter 4. Input and Output
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 Excel
Problem
Solution
Discussion
See Also
4.12 Writing a Data Frame to Excel
Problem
Solution
Discussion
See Also
4.13 Reading Data from a SAS File
Problem
Solution
Discussion
See Also
4.14 Reading Data from HTML Tables
Problem
Solution
Discussion
See Also
4.15 Reading Files with a Complex Structure
Problem
Solution
Discussion
An Example
4.16 Reading from MySQL Databases
Problem
Solution
Discussion
See Also
4.17 Accessing a Database with dbplyr
Problem
Solution
Discussion
See Also
4.18 Saving and Transporting Objects
Problem
Solution
Discussion
See Also
Chapter 5. Data Structures
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 Flattening 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 Selecting Data Frame Columns by Position
Problem
Solution
Discussion
See Also
5.22 Selecting Data Frame Columns by Name
Problem
Solution
Discussion
See Also
5.23 Changing the Names of Data Frame Columns
Problem
Solution
Discussion
See Also
5.24 Removing NAs from a Data Frame
Problem
Solution
Discussion
5.25 Excluding Columns by Name
Problem
Solution
Discussion
5.26 Combining Two Data Frames
Problem
Solution
Discussion
5.27 Merging Data Frames by Common Column
Problem
Solution
Discussion
See Also
5.28 Converting One Atomic Value into Another
Problem
Solution
Discussion
5.29 Converting One Structured Data Type into Another
Problem
Solution
Discussion
See Also
Chapter 6. Data Transformations
6.1 Applying a Function to Each List Element
Problem
Solution
Discussion
See Also
6.2 Applying a Function to Every Row of a Data Frame
Problem
Solution
Discussion
6.3 Applying a Function to Every Row of a Matrix
Problem
Solution
Discussion
6.4 Applying a Function to Every Column
Problem
Solution
Discussion
See Also
6.5 Applying a Function to Parallel Vectors or Lists
Problem
Solution
Discussion
See Also
6.6 Applying a Function to Groups of Data
Problem
Solution
Discussion
6.7 Creating a New Column Based on Some Condition
Problem
Solution
Discussion
See Also
Chapter 7. Strings and Dates
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 Generating All Pairwise Combinations of Strings
Problem
Solution
Discussion
See Also
7.7 Getting the Current Date
Problem
Solution
Discussion
See Also
7.8 Converting a String into a Date
Problem
Solution
Discussion
7.9 Converting a Date into a String
Problem
Solution
Discussion
7.10 Converting Year, Month, and Day into a Date
Problem
Solution
Discussion
7.11 Getting the Julian Date
Problem
Solution
Discussion
7.12 Extracting the Parts of a Date
Problem
Solution
Discussion
7.13 Creating a Sequence of Dates
Problem
Solution
Discussion
Chapter 8. Probability
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
Chapter 9. General Statistics
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
Chapter 10. Graphics
10.1 Creating a Scatter Plot
Problem
Solution
Discussion
See Also
10.2 Adding a Title and Labels
Problem
Solution
Discussion
10.3 Adding (or Removing) a Grid
Problem
Solution
Discussion
See Also
10.4 Applying a Theme to a ggplot Figure
Problem
Solution
Discussion
See Also
10.5 Creating a Scatter Plot of Multiple Groups
Problem
Solution
Discussion
See Also
10.6 Adding (or Removing) a Legend
Problem
Solution
Discussion
10.7 Plotting the Regression Line of a Scatter Plot
Problem
Solution
Discussion
See Also
10.8 Plotting All Variables Against All Other Variables
Problem
Solution
Discussion
10.9 Creating One Scatter Plot for Each Group
Problem
Solution
Discussion
See Also
10.10 Creating a Bar Chart
Problem
Solution
Discussion
See Also
10.11 Adding Confidence Intervals to a Bar Chart
Problem
Solution
Discussion
See Also
10.12 Coloring a Bar Chart
Problem
Solution
Discussion
See Also
10.13 Plotting a Line from x and y Points
Problem
Solution
Discussion
See Also
10.14 Changing the Type, Width, or Color of a Line
Problem
Solution
Discussion
See Also
10.15 Plotting Multiple Datasets
Problem
Solution
Discussion
10.16 Adding Vertical or Horizontal Lines
Problem
Solution
Discussion
See Also
10.17 Creating a Boxplot
Problem
Solution
Discussion
See Also
10.18 Creating One Boxplot for Each Factor Level
Problem
Solution
Discussion
See Also
10.19 Creating a Histogram
Problem
Solution
Discussion
See Also
10.20 Adding a Density Estimate to a Histogram
Problem
Solution
Discussion
See Also
10.21 Creating a Normal Quantile–Quantile 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 Displaying Several Figures on One Page
Problem
Solution
Discussion
See Also
10.26 Writing Your Plot to a File
Problem
Solution
Discussion
See Also
Chapter 11. Linear Regression and ANOVA
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 Regressing Only Variables That Highly Correlate with Your Dependent Variable
Problem
Solution
Discussion
11.7 Performing Linear Regression with Interaction Terms
Problem
Solution
Discussion
See Also
11.8 Selecting the Best Regression Variables
Problem
Solution
Discussion
See Also
11.9 Regressing on a Subset of Your Data
Problem
Solution
Discussion
11.10 Using an Expression Inside a Regression Formula
Problem
Solution
Discussion
See Also
11.11 Regressing on a Polynomial
Problem
Solution
Discussion
See Also
11.12 Regressing on Transformed Data
Problem
Solution
Discussion
See Also
11.13 Finding the Best Power Transformation (Box–Cox Procedure)
Problem
Solution
Discussion
See Also
11.14 Forming Confidence Intervals for Regression Coefficients
Problem
Solution
Discussion
See Also
11.15 Plotting Regression Residuals
Problem
Solution
Discussion
See Also
11.16 Diagnosing a Linear Regression
Problem
Solution
Discussion
See Also
11.17 Identifying Influential Observations
Problem
Solution
Discussion
See Also
11.18 Testing Residuals for Autocorrelation (Durbin–Watson Test)
Problem
Solution
Discussion
See Also
11.19 Predicting New Values
Problem
Solution
Discussion
See Also
11.20 Forming Prediction Intervals
Problem
Solution
Discussion
11.21 Performing One-Way ANOVA
Problem
Solution
Discussion
See Also
11.22 Creating an Interaction Plot
Problem
Solution
Discussion
See Also
11.23 Finding Differences Between Means of Groups
Problem
Solution
Discussion
See Also
11.24 Performing Robust ANOVA (Kruskal–Wallis Test)
Problem
Solution
Discussion
11.25 Comparing Models by Using ANOVA
Problem
Solution
Discussion
Chapter 12. Useful Tricks
12.1 Peeking at Your Data
Problem
Solution
Discussion
See Also
12.2 Printing the Result of an Assignment
Problem
Solution
Discussion
See Also
12.3 Summing Rows and Columns
Problem
Solution
Discussion
12.4 Printing Data in Columns
Problem
Solution
Discussion
12.5 Binning Your Data
Problem
Solution
Discussion
12.6 Finding the Position of a Particular Value
Problem
Solution
Discussion
See Also
12.7 Selecting Every nth Element of a Vector
Problem
Solution
Discussion
See Also
12.8 Finding Minimums or Maximums
Problem
Solution
Discussion
See Also
12.9 Generating All Combinations of Several Variables
Problem
Solution
Discussion
See Also
12.10 Flattening a Data Frame
Problem
Solution
Discussion
See Also
12.11 Sorting a Data Frame
Problem
Solution
Discussion
12.12 Stripping Attributes from a Variable
Problem
Solution
Discussion
See Also
12.13 Revealing the Structure of an Object
Problem
Solution
Discussion
12.14 Timing Your Code
Problem
Solution
Discussion
12.15 Suppressing Warnings and Error Messages
Problem
Solution
Discussion
See Also
12.16 Taking Function Arguments from a List
Problem
Solution
Discussion
See Also
12.17 Defining Your Own Binary Operators
Problem
Solution
Discussion
See Also
12.18 Suppressing the Startup Message
Problem
Solution
Discussion
12.19 Getting and Setting Environment Variables
Problem
Solution
Discussion
See Also
12.20 Use Code Sections
Problem
Solution
Discussion
See Also
12.21 Executing R in Parallel Locally
Problem
Solution
Discussion
See Also
12.22 Executing R in Parallel Remotely
Problem
Solution
Discussion
See Also
Chapter 13. Beyond Basic Numerics and Statistics
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
Chapter 14. Time Series Analysis
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 Plotting a Forecast
Problem
Solution
Discussion
See Also
14.23 Testing for Mean Reversion
Problem
Solution
Discussion
See Also
14.24 Smoothing a Time Series
Problem
Solution
Discussion
See Also
Chapter 15. Simple Programming
15.1 Choosing Between Two Alternatives: if/else
Problem
Solution
Discussion
See Also
15.2 Iterating with a Loop
Problem
Solution
Discussion
See Also
15.3 Defining a Function
Problem
Solution
Discussion
See Also
15.4 Creating a Local Variable
Problem
Solution
Discussion
15.5 Choosing Between Multiple Alternatives: switch
Problem
Solution
Discussion
See Also
15.6 Defining Defaults for Function Parameters
Problem
Solution
Discussion
15.7 Signaling Errors
Problem
Solution
Discussion
See Also
15.8 Protecting Against Errors
Problem
Solution
Discussion
See Also
15.9 Creating an Anonymous Function
Problem
Solution
Discussion
See Also
15.10 Creating a Collection of Reusable Functions
Problem
Solution
Discussion
See Also
15.11 Automatically Reindenting Code
Problem
Solution
Discussion
See Also
Chapter 16. R Markdown and Publishing
16.1 Creating a New Document
Problem
Solution
Discussion
16.2 Adding a Title, Author, or Date
Problem
Solution
Discussion
See Also
16.3 Formatting Document Text
Problem
Solution
See Also
16.4 Inserting Document Headings
Problem
Solution
Discussion
16.5 Inserting a List
Problem
Solution
Discussion
See Also
16.6 Showing Output from R Code
Problem
Solution
Discussion
See Also
16.7 Controlling Which Code and Results Are Shown
Problem
Solution
Discussion
See Also
16.8 Inserting a Plot
Problem
Solution
Discussion
16.9 Inserting a Table
Problem
Solution
Discussion
See Also
16.10 Inserting a Table of Data
Problem
Solution
Discussion
16.11 Inserting Math Equations
Problem
Solution
Discussion
16.12 Generating HTML Output
Problem
Solution
Discussion
See Also
16.13 Generating PDF Output
Problem
Solution
Discussion
See Also
16.14 Generating Microsoft Word Output
Problem
Solution
Discussion
See Also
16.15 Generating Presentation Output
Problem
Solution
Discussion
See Also
16.16 Creating a Parameterized Report
Problem
Solution
Discussion
See Also
16.17 Organizing Your R Markdown Workflow
Problem
Solution
See Also
Index
About the Authors