Full Table of Contents

A Survivor’s Guide to R

The Author

Preface

Acknowledgements

Summary Table of Contents

Full Table of Contents

Tables

Figures

Chapter 1 Getting started

1.1 Things your statistics class probably won’t teach you
1.2 Why R?
1.3 Statistical modeling
1.4 A few R basics
1.4a Running R and inputting commands
1.4b Comments
1.4c Command conventions
1.4d Parentheses ( ), brackets [ ], braces { }
1.4e Comparison operators
1.5 Saving your work
1.6 R packages
1.7 Help with R help
1.7a Help with R packages
1.7b Error messages
1.7c Help from the R community
1.8 Organization of this book

Chapter 2 A sample session

2.1 Reviewing your data
2.2 Data visualization
2.3 Hypothesis testing
2.4 A regression model
2.5 A nonlinear model

Chapter 3 Object types in R

3.1 R objects and their names
3.2 How to think about data objects in R
3.3 R object storage modes
3.3a The character storage mode
3.3b The numeric storage modes
3.3c The logical storage mode
3.4 R data object types
3.5 The basic data objects: Vectors
3.5a Vector indices
3.5b Vector operations
3.6 The basic data objects: Matrices and their indices
3.7 The basic data objects: Data frames
3.7a Referencing data frame elements
3.7b Displaying the contents of a data frame
3.8 The basic data objects: Lists
3.9 A few things about working with objects
3.10 Object attributes
3.11 Objects and environments
3.12 R object classes
3.13 The pseudo storage modes
3.14 Date and time as a storage modes
3.15 Factors
3.16 Coercing storage modes
3.17 The curse of number-character-factor confusion
3.18 Conclusions

Chapter 4 Getting your data into R

4.1 Entering data
4.1a Entering data with the concatenate functions
4.1b Joining vectors intro matrices and data frames
4.1c Entering data with the R spreadsheet
4.2 Creating data
4.2a Simple sequences and repetitions
4.2b Generating factors
4.2c Random numbers and statistical distributions
4.3 Importing data
4.3a Working with the working directory
4.4 The read command: Overview
4.5 The read command: Reading from the clipboard
4.6 The read command: Blank delimited tables
4.7 The read command: Comma separated values
4.8 The read command: Tab separated data
4.9 The read command: Fixed-width data
4.10 Importing foreign file types
4.10a Exporting data in foreign formats
4.11 Integrating SQL with R
4.12 Extracting data from complex data sources
4.13 Web scraping
4.14 Dealing with multi-dimensional data
4.15 Importing problematic characters
4.16 More resources

Chapter 5 Reviewing and summarizing data

5.1 Summary functions
5.2 Checking a sample of your data
5.3 Reviewing data by categories
5.4 Displaying data with a histogram
5.5 Displaying data with a scatter plot
5.6 Scatter plot matrices

Chapter 5 Appendix In case of exasperation, read this!

A .1 The big things
A .2 The little things

Chapter 6 Sorting and selecting data

6.1 Using index values to select data
6.2 Using conditional values for selecting
6.3 Using subset( ) with variable or row names to select data
6.4 Splitting a dataset into groups
6.5 Splitting up continuous numeric data
6.6 Sorting and ordering data
6.6a Sorting a variable
6.6b Ordering a data frame

Chapter 7 Transforming data

7.1 Creating new variables
7.2 Editing data
7.3 Basic math with R
7.4 R functions
7.5 Math and logical functions in R
7.6 Truncation and rounding functions
7.7 The apply( ) family of functions
7.8 Changing variable values conditionally
7.9 Creating new functions
7.10 Additional R programming
7.10a Loops
7.10b If( ){ } else{ }
7.11 Character strings as program elements and program elements as character strings
7.11a Dynamic object names
7.11b Dynamic R code

Chapter 8 Text operations

8.1 Some useful text functions
8.2 Finding things
8.3 Regular expressions
8.3a Location operators
8.3b The repetition operators
8.3c Other pattern operators
8.3d A wisdom of Perl
8.4 Processing raw text data
8.5 Scraping the web for fun and profit

Chapter 9 Working with date and time data

9.1 Dates in R
9.2 Formatting dates for R
9.3 Working with POSIX dates
9.4 Special date operations
9.5 Formatting dates for output
9.6 Time series data
9.7 Creating moving averages in time-series data
9.8 Lagged variables in time-series data
9.9 Differencing variables in time-series data
9.10 The limitations of ts data

Chapter 10 Data merging and aggregation

10.1 Dataset concatenation
10.1a Adding new variables: Column concatenation
10.1b Adding new observations: Row concatenation
10.2 Match merging
10.3 Keyed table look-up merging
10.4 Aggregating data
10.5 Transposing and rotating datasets

Chapter 11 Dealing with missing data

11.1 Reading data with missing values
11.2 Summarizing missing values
11.3 The missing values functions
11.4 Recoding missing values
11.5 Missing values and regression modeling
11.6 Visualizing missing data

Chapter 12 R graphics I: The built-in plots

12.1 Scatter plots
12.2 Pairs plots
12.3 Line plots
12.4 Box plots
12.5 Histograms, density plots, and bar charts
12.6 Dot charts
12.7 Pie charts
12.8 Mosaic plots
12.9 Conclusions

Chapter 13 R graphics II: The boring stuff

13.1 The graphics device
13.1a Screen-based devices
13.1b Raster/bitmap devices
13.1c Vector-based devices
13.2 Graphics parameters
13.3 The plot layout
13.4 Graphic coordinates in R
13.5 Overlaying plots
13.6 Multiple plots
13.7 Conclusions

Chapter 14 R graphics III: The fun stuff–Text

14.1 Adding text
14.2 Setting up a font
14.2a The built-in fonts
14.2b The font-mapping approach
14.2c The Rdevga approach to font mapping
14.2d Font size and rotation
14.3 Titles and subtitles
14.4 Creating a legend
14.5 Simple axes and axis labels
14.6 Building more complex axes
14.6a Tick marks
14.6b Axes with dates or times
14.7 Ad-hoc text

Chapter 15 R graphics III: The fun stuff–Shapes

15.1 Doing colors
15.1a Specifying colors
15.1b Colors and transparency
15.2 Custom points
15.2a Connecting the dots
15.2b Plotting with symbols()
15.3 Adding lines
15.3a Basic lines
15.3b Line segments
15.3c Arrows
15.3d Grid lines
15.4 Shapes
15.5 Incorporating images into plots
15.6 A final word about aesthetics

Chapter 16 From here to where?

Appendix I Installing R and related programs

AI.1 Installing R plain and simple
AI.2 R extras
AI.3 Startup configuration for R
AI.4 R packages
AI.5 Updating R
AI.6 Running R and RStudio from a USB key

Appendix II Statistical commands

AII.1 Univariate descriptive statistics
AII.2 Univariate data visualization
AII.3 Bivariate descriptives: Measures of association
AII.4 Hypothesis testing
AII.5 Cross tabulation
AII.6 ANOVA
AII.7 Linear regression

Appendix III R packages

AIII.1 Data management packages
AIII.2 Graphics packages
AIII.3 Miscellaneous packages

Appendix IV Some style guidelines

AIV.1 Modularity and program structure
AIV.2 Argument identification
AIV.3 Comments
AIV.4 White space: Blank lines and indentation
AIV.5 Placement of parentheses and braces
AIV.6 Naming conventions
AIV.7 The bottom line: Clarity and replicability

References

Data Sources

Subjext Index

Index of R Commands