The R Primer provides a collection of concise examples with solutions and interpretations of R problems frequently encountered by new users of this statistical software. The R Primer contains numerous examples that illustrate a specific situation, topic, or problem, including data importing, data management, classical statistical analyses, and high-quality graphical production. Each example is self-contained and includes R code that can be run exactly as shown, enabling results from the book to be replicated.

New to the Second Edition:

- Completely revised and updated with suggestions for using new and improved R packages
- Expanded with over 100 more pages
- New solutions for covering areas from web scraping over data wrangling to waffle plots and hanging rootograms.
- Additional intermediate and advanced topics in statistical data analysis including non-parametric statistics, random forests, penalized regression and curve smoothing.

The site contains source code, extra material and errata.

- Presents commonly encountered problems and solutions in R in a “cookbook” style
- Covers importing and transforming data, making statistical analyses, and creating graphics
- Provides an ideal introduction for beginning users of R
- Includes code for all examples

“This book provides a good introduction to R, using a clear layout and detailed, reproducible examples. An ideal tool for any new R user. … A wide range of topics are covered, making the book suitable for a variety of readers, from undergraduate students to professionals new to R … an extremely helpful introduction to a very useful statistical package.”

- Claire Keeble, Journal of Applied Statistics, 2012

- Preface
- Importing and exporting data
- Import an R dataset from a package
- Load and save R data files
- Read and write text files
- Read and write CSV files
- Read data from the clipboard
**Spreadsheets**- Read and write data from a spreadsheet
- Read and write Excel files
**Statistical software programs**- Import and export SAS datasets
- Import and export SPSS datasets
- Import or export a Stata dataset
- Import a Systat dataset
**Data exchange formats and databases**- Import a JSON dataset
- Read data from a simple XML file
- Read data from an XML file
- Export a data frame to XML
- Import data from an HTML table
- Scrape data from an HTML web page
- Import from a MySQL/PostgreSQL database
- Read data from an SQL database using ODBC

- Manipulating data
- Use mathematical functions
- Use common vector operations
- Work with character vectors
- Read non-ASCII character vectors
- Sort and order data
- Transform a variable
- Find the value of \(x\) corresponding to the maximum or minimum of \(y\)
- Check if elements in one object are present in another object
- Apply a function to subsets of a vector
- Fill in missing values with previous values
- Convert comma as decimal mark to period
- Lag or shift a vector
- Calculate the area under a curve
**Matrices, arrays, and tables**- Apply function to margins of a matrix/array
- Compute a matrix/array of proportions
- Transpose a matrix (or data frame)
- Create a table of counts
- Convert a table of counts to a data frame
**Dates and times**- Parsing dates and times
- Extract and format date/time information
**Factors**- Convert a factor to numeric
- Convert a factor to character strings
- Add a new level to an existing factor
- Combine the levels of a factor
- Remove unused levels of a factor
- Change the reference level
- Cut a numeric vector into a factor
**Data frames and lists**- Select a subset of a data frame
- Select the complete cases of a data frame
- Delete a variable from a data frame
- Apply function to each variable in a data frame
- Split data frame into subsets and apply function to each part
- Apply function to each row of a data frame
- Combine two datasets
- Merge datasets
- Add new observations to a data frame
- Stack the columns of a data frame together
- Reshape a data frame from wide to long format or vice versa
- Convert a data frame to a vector

- Statistical analyses
**Descriptive statistics**- Compute summary statistics
- Create descriptive table
**Linear models**- Fit a linear regression model
- Fit a multiple linear regression model
- Fit a polynomial regression model
- Fit a one-way analysis of variance
- Make post-hoc pairwise comparisons
- Fit a two-way analysis of variance
- Fit a linear normal model
- Fit a penalized regression model
**Generalized linear models**- Fit a logistic regression model
- Fit a conditional logistic regression model
- Fit an ordinal logistic regression model
- Fit a multinomial logistic regression model
- Fit a Poisson regression model
**Methods for analysis of repeated measurements**- Fit a linear mixed-effects model
- Fit a linear mixed-effects model with serial correlation
- Fit a generalized linear mixed model
- Fit a generalized estimating equation model
- Analyze time series using an ARIMA model
- Decompose a time series into trend, seasonal, and residual components
**Specific methods**- Compare populations using \(t\) test
- Fit a nonlinear model
- Fit a censored regression model
- Fit a zero-inflated regression model
- Fit a smooth curve
- Fit a generalized additive model
- Use meta analysis to combine and summarize the results from several studies
- Use random forest for classification and regression
- Fit a linear quantile regression model
**Model validation**- Test for normality of a single sample
- Test variance homogeneity across groups
- Validate a linear or generalized linear model
**Contingency tables**- Analyze two-dimensional contingency tables
- Analyze two-dimensional contingency tables with ordinal categories
- Analyze two-dimensional contingency tables with paired measurements
- Analyze contingency tables using log-linear models
**Agreement**- Create a Bland–Altman plot of agreement to compare two quantitative methods
- Determine agreement among several methods of a quantitative measurement
- Calculate Cohen’s kappa for agreement
**Multivariate methods**- Fit a multivariate regression model
- Cluster observations
- Use principal component analysis to reduce data dimensionality
- Fit a principal component regression model
- Classify+\thinmuskip {.1667em observations+\thinmuskip {.1667em using+\thinmuskip {.1667em linear+\thinmuskip {.1667em discriminant +\thinmuskip {.1667em analysis
- Use partial least squares regression for prediction
**Resampling statistics and bootstrapping**- Non-parametric bootstrap analysis
- Use cross-validation to estimate the performance of a model or algorithm
- Calculate power or sample size for simple designs
**Non-parametric methods**- Test median with Wilcoxon’s signed rank test
- Use Mann–Whitney’s test to compare two groups
- Compare groups using Kruskal–Wallis’ test
- Compare groups using Friedman’s test for a two-way design
**Survival analysis**- Fit a Kaplan–Meier survival curve to event history data
- Fit a Cox regression model (proportional hazards model)
- Fit a Cox regression model (proportional hazards model) with time-varying covariates
**Miscellaneous**- Correct \(p\)-values for multiple testing
- Use a Box–Cox transformation to make non-normally distributed data approximately normal

- Graphics
- Use Greek letters and formulas in graphs
- Set colors in R graphics
- Set color palettes in R graphics
**High-level plots**- Create a scatter plot
- Create a bubble plot
- Create a histogram
- Create a hanging rootogram
- Create a violin- or boxplot
- Create a bar plot
- Create a bar plot with error bars
- Create a plot with estimates and confidence intervals
- Create a pie chart
- Create a pyramid plot
- Plot multiple series
- Make a 2D surface plot
- Make a 3D surface plot
- Plot a 3D scatter plot
- Create a heat map plot
- Plot a correlation matrix
- Make a quantile-quantile plot
- Graphical model validation for linear models
- Create a Venn or Euler diagram
**Tweaking graphics**- Add a broken axis to indicate discontinuity
- Create a plot with two \(y\)-axes
- Rotate axis labels
- Multiple plots
- Add a legend to a plot
- Add a table to a plot
- Label points in a scatter plot
- Identify points in a scatter plot
- Visualize points, shapes, and surfaces in 3D and interact with them in real-time
**Working with graphics**- Exporting graphics
- Produce graphics in LaTeX-ready format
- Embed fonts in postscript or pdf graphics

- R
**Getting information**- Getting help
- Finding R source code for a function
- Timing R commands and functions
**R packages**- Installing R packages
- Update installed R packages
- Unload a package
- Install an R package from a repository
- Install a package from BioConductor
- List the installed packages
- List the content of a package
- Uninstall an R package
- List or view vignettes
- Permanently change the default directory where R installs packages
- Automatically load a package at startup
**The R workspace**- List or delete objects
- Change the current working directory
- Save and load workspaces
- Save and restore histories
- Interact with the file system
- Locate and choose files interactively
- Interact with the operating system
- Get session information

- Bibliography
- Index

Created by Claus Ekstrøm 2017, page last updated 29 December, 2017