Introduction
Using R for Data Analysis and Graphics (PDF file) is a tutorial written by John Maindonald, Centre for Bioinformation Science, Australian National University. Professor Maindonald also provides the scripts and data set that accompany Using R.
Note: John Maindonald and John Braun are coauthors of the book Data Analysis and Graphics Using R; see my notes for more information.
This page stores my notes as I work through the chapters of this book. I am using R 2.2.1 and R GUI 1.14 for Mac OS X.
Setting up a working directory
Download the Using R
for Data Analysis and Graphics PDF file, the
scripts, and the
UsingR.RData
data set to a working directory. For these notes, I have used the directory
/Volumes/CHHalling/R/Tutorials/Maindonald.
Loading the UsingR.RData file
Attach the UsingR.RData data file and list the objects it contains:
setwd( dir = "/Volumes/CHHalling/R/Tutorials/Maindonald" ) attach( what = "UsingR.RData" ) ls( name = "file:UsingR.RData" )
A more conventional way to load the data file so that the data objects are in the user’s work space is with the following commands:
setwd( dir = "/Volumes/CHHalling/R/Tutorials/Maindonald" ) load( file = "UsingR.RData" ) ls()
It is also possible to load the UsingR.RData data file via a URL
connection without having a local copy on your computer. The commands are:
setwd( dir = "/Volumes/CHHalling/R/Tutorials/Maindonald" ) con <- url( "http://www.maths.anu.edu.au/~johnm/r/dsets/usingR.RData" ) load( con ) close( con ) ls()
Chapter 1
The R script ch1-2.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
According to Data Analysis and Graphics Using R by John Maindonald
and John Braun, p. 3, the elasticband data are the results of a rubber band
shooting experiment, where stretch is the length that the
rubber band was stretched before shooting, and distance is the distance
the rubber band traveled when it was released.
These are my solutions to the exercises:
## Set the working directory and attach the data file.
setwd( dir = "/Volumes/CHHalling/R/Tutorials/Maindonald" )
attach( what = "UsingR.RData" )
## Exercise 1
elasticband <-
data.frame(
stretch = c( 46, 54, 48, 50, 44, 42, 52 ),
distance = c( 148, 182, 173, 166, 109, 141, 166 ) )
plot( distance ~ stretch, data = elasticband )
## Exercise 2
snowCover <-
data.frame(
year = 1970:1979,
snow.cover = c( 6.5, 12.0, 14.9, 10.0, 10.7,
7.9, 21.9, 12.5, 14.5, 9.2 ) )
get( getOption( "device" ) )()
plot( snow.cover ~ year, data = snowCover )
get( getOption( "device" ) )()
plot( log( snow.cover ) ~ year, data = snowCover )
## Exercise 3
damage <-
data.frame(
temperature = c( 53, 57, 63, 70, 70, 75 ),
erosion = c( 3, 1, 1, 1, 1, 0 ),
blowby = c( 2, 0, 0, 0, 0, 2 ),
total = c( 5, 1, 1, 1, 1, 2 ) )
get( getOption( "device" ) )()
plot( total ~ temperature, data = damage )
## The entire data set is provided in data.frame orings from the
## data file UsingR.RData. Plot these data.
get( getOption( "device" ) )()
plot( Total ~ Temperature, data = orings )
Chapter 2
The R script ch1-2.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
## Set the working directory and attach the data file. setwd( dir = "/Volumes/CHHalling/R/Tutorials/Maindonald" ) attach( what = "UsingR.RData" )
Chapter 3
The R script ch3-4.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 4
The R script ch3-4.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 5
The R script ch5.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 6
The R script ch6.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 7
The R script ch7.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 8
The R script ch8.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 9
The R script ch9.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 10
The R script ch10.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.
Chapter 11
The R script ch11.R as provided cannot be sourced successfully. Make the
following change:
Change line 32 from
library(mass) # if needed
to
library(MASS) # if needed
Run the script:
setwd( dir = "/Volumes/CHHalling/R/Tutorials/Maindonald" ) attach( what = "UsingR.RData" ) source( file = "ch11.R", echo = TRUE )
R produces the following warning message when the script is sourced:
Warning message: Error() model is singular in: aov(yield ~ block + shade + Error(block:shade), data = kiwishade)
Chapter 12
The R script ch12.R as provided cannot be sourced successfully. Instead, open
it in a text editor and copy and paste commands into R.