Use R!
A Beginner’s Guide to R
Preface
The Absolute R Beginner
Datasets used in This book
Acknowledgements
Contents
Introduction
1.1 What Is R?
1.2 Downloading and Installing R
1.3 An Initial Impression
1.4 Script Code
1.4.1 The Art of Programming
1.4.2 Documenting Script Code
1.5 Graphing Facilities in R
1.6 Editors
1.7 Help Files and Newsgroups
1.8 Packages
1.8.1 Packages Included with the Base Installation
1.8.2 Packages Not Included with the Base Installation
1.8.2.1 Option 1. Manual Download and Installation
1.8.2.2 Option 2. Download and Install a Package from Within R
1.8.2.1 Loading the Package
1.8.2.2 How Good Is a Package?
1.9 General Issues in R
1.9.1 Quitting R and Setting the Working Directory
1.10 A History and a Literature Overview
1.10.1 A Short Historical Overview of R
1.10.2 Books on R and Books Using R
1.10.2.1 The Use R! Series
1.11 Using This Book
1.11.1 If You Are an Instructor
1.11.2 If You Are an Interested Reader with Limited R Experience
1.11.3 If You Are an R Expert
1.11.4 If You Are Afraid of R
1.12 Citing R and Citing Packages
1.13 Which R Functions Did We Learn?
Getting Data into R
2.1 First Steps in R
2.1.1 Typing in Small Datasets
2.1.2 Concatenating Data with the c Function
2.1.3 Combining Variables with the c, cbind, and rbind Functions
2.1.4 Combining Data with the vector Function*
2.1.5 Combining Data Using a Matrix*
2.1.6 Combining Data with the data.frame Function
2.1.7 Combining Data Using the list Function*
2.2 Importing Data
2.2.1 Importing Excel Data
2.2.1.1 Prepare the Data in Excel
2.2.1.2 Export Data to a Tab-Delimited ascii File
2.2.1.3 Using the read.table Function
2.2.2 Accessing Data from Other Statistical Packages**
2.2.3 Accessing a Database***
2.3 Which R Functions Did We Learn?
2.4 Exercises
Accessing Variables and Managing Subsets of Data
3.1 Accessing Variables from a Data Frame
3.1.1 The str Function
3.1.2 The Data Argument in a Function
3.1.3 The $ Sign
3.1.4 The attach Function
3.2 Accessing Subsets of Data
3.2.1 Sorting the Data
3.3 Combining Two Datasets with a Common Identifier
3.4 Exporting Data
3.5 Recoding Categorical Variables
3.6 Which R Functions Did We Learn?
3.7 Exercises
Simple Functions
4.1 The tapply Function
4.1.1 Calculating the Mean Per Transect
4.1.2 Calculating the Mean Per Transect More Efficiently
4.2 The sapply and lapply Functions
4.3 The summary Function
4.4 The table Function
4.5 Which R Functions Did We Learn?
4.6 Exercises
An Introduction to Basic Plotting Tools
5.1 The plot Function
5.2 Symbols, Colours, and Sizes
5.2.1 Changing Plotting Characters
5.2.1.1 Use of a Vector for pch
5.2.2 Changing the Colour of Plotting Symbols
5.2.2.1 Use of a Vector for col
5.2.3 Altering the Size of Plotting Symbols
5.2.3.1 Use of a Vector for cex
5.3 Adding a Smoothing Line
5.4 Which R Functions Did We Learn?
5.5 Exercises
Loops and Functions
6.1 Introduction to Loops
6.2 Loops
6.2.1 Be the Architect of Your Code
6.2.2 Step 1: Importing the Data
6.2.3 Steps 2 and 3: Making the Scatterplot and Adding Labels
6.2.4 Step 4: Designing General Code
6.2.5 Step 5: Saving the Graph
6.2.6 Step 6: Constructing the Loop
6.3 Functions
6.3.1 Zeros and NAs
6.3.2 Technical Information
6.3.3 A Second Example: Zeros and NAs
6.3.4 A Function with Multiple Arguments
6.3.5 Foolproof Functions
6.3.5.1 Default Values for Variables in Function Arguments
6.3.5.2 Misspelling
6.4 More on Functions and the if Statement
6.4.1 Playing the Architect Again
6.4.2 Step 1: Importing and Assessing the Data
6.4.3 Step 2: Total Abundance per Site
6.4.4 Step 3: Richness per Site
6.4.5 Step 4: Shannon Index per Site
6.4.6 Step 5: Combining Code
6.4.7 Step 6: Putting the Code into a Function
6.5 Which R Functions Did We Learn?
6.6 Exercises
Graphing Tools
7.1 The Pie Chart
7.1.1 Pie Chart Showing Avian Influenza Data
7.1.2 The par Function
7.2 The Bar Chart and Strip Chart
7.2.1 The Bar Chart Using the Avian Influenza Data
7.2.2 A Bar Chart Showing Mean Values with Standard Deviations
7.2.3 The Strip Chart for the Benthic Data
7.3 Boxplot
7.3.1 Boxplots Showing the Owl Data
7.3.2 Boxplots Showing the Benthic Data
7.4 Cleveland Dotplots
7.4.1 Adding the Mean to a Cleveland Dotplot
7.5 Revisiting the plot Function
7.5.1 The Generic plot Function
7.5.2 More Options for the plot Function
7.5.3 Adding Extra Points, Text, and Lines
7.5.4 Using type = ’’n’’
7.5.5 Legends
7.5.6 Identifying Points
7.5.7 Changing Fonts and Font Size*
7.5.8 Adding Special Characters
7.5.9 Other Useful Functions
7.6 The Pairplot
7.6.1 Panel Functions
7.7 The Coplot
7.7.1 A Coplot with a Single Conditioning Variable
7.7.2 The Coplot with Two Conditioning Variables
7.7.3 Jazzing Up the Coplot*
7.8 Combining Types of Plots*
7.9 Which R Functions Did We Learn?
7.10 Exercises
An Introduction to the Lattice Package
8.1 High-Level Lattice Functions
8.2 Multipanel Scatterplots: xyplot
8.3 Multipanel Boxplots: bwplot
8.4 Multipanel Cleveland Dotplots: dotplot
8.5 Multipanel Histograms: histogram
8.6 Panel Functions
8.6.1 First Panel Function Example
8.6.2 Second Panel Function Example
8.6.3 Third Panel Function Example*
8.7 3-D Scatterplots and Surface and Contour Plots
8.8 Frequently Asked Questions
8.8.1 How to Change the Panel Order?
8.8.2 How to Change Axes Limits and Tick Marks?
8.8.3 Multiple Graph Lines in a Single Panel
8.8.4 Plotting from Within a Loop*
8.8.5 Updating a Plot
8.9 Where to Go from Here?
8.10 Which R Functions Did We Learn?
8.11 Exercises
Common R Mistakes
9.1 Problems Importing Data
9.1.1 Errors in the Source File
9.1.2 Decimal Point or Comma Separation
9.1.3 Directory Names
9.2 Attach Misery
9.2.1 Entering the Same attach Command Twice
9.2.2 Attaching Two Data Frames Containing the Same Variable Names
9.2.3 Attaching a Data Frame and Demo Data
9.2.4 Making Changes to a Data Frame After Applying the attach Function
9.3 Non-attach Misery
9.4 The Log of Zero
9.5 Miscellaneous Errors
9.5.1 The Difference Between 1 and l
9.5.2 The Colour of 0
9.5.3 Mistakenly Saved the R Workspace
References
Index