R
Introduction
R.M. Ripley
Department of Statistics
University of Oxford
2012/13
R.M. Ripley (University of Oxford) R 2012/13 1 / 17
What is R
What is R?
What can you do using R?
Data entry and manipulation
Input data
. . . from keyboard
. . . from spreadsheet
. . . from another statistics package
Statistical analysis
Summaries: mean, variance, density
Linear modelling (and Generalised Linear and Non-Linear)
Many more exotic methods
R.M. Ripley (University of Oxford) R 2012/13 2 / 17
What is R
What is R?
What can you do using R?
Data entry and manipulation
Input data
. . . from keyboard
. . . from spreadsheet
. . . from another statistics package
Statistical analysis
Summaries: mean, variance, density
Linear modelling (and Generalised Linear and Non-Linear)
Many more exotic methods
R.M. Ripley (University of Oxford) R 2012/13 2 / 17
What is R
More possibilities with R
Graphical display
Predefined plots for some models
Flexible, powerful options
Save to image files in various formats
Write new functions
Make a change to an existing function
Create new functions tailored to your exact needs
Contribute a new package
R.M. Ripley (University of Oxford) R 2012/13 3 / 17
What is R
More possibilities with R
Graphical display
Predefined plots for some models
Flexible, powerful options
Save to image files in various formats
Write new functions
Make a change to an existing function
Create new functions tailored to your exact needs
Contribute a new package
R.M. Ripley (University of Oxford) R 2012/13 3 / 17
What is R
More possibilities with R
Graphical display
Predefined plots for some models
Flexible, powerful options
Save to image files in various formats
Write new functions
Make a change to an existing function
Create new functions tailored to your exact needs
Contribute a new package
R.M. Ripley (University of Oxford) R 2012/13 3 / 17
What is R
More possibilities with R
Graphical display
Predefined plots for some models
Flexible, powerful options
Save to image files in various formats
Write new functions
Make a change to an existing function
Create new functions tailored to your exact needs
Contribute a new package
R.M. Ripley (University of Oxford) R 2012/13 3 / 17
What is R
Starting and Stopping R
Create a short-cut to start in
your project folder
If there is a short-cut on your
desk top, but you do not have
permission to alter it, try to
create a new one:
Right-click, select Create
Shortcut.
Do the same to the new
short-cut.
Rename the second copy
and delete the first one.
Tailor the short-cut
Right-click on the short-cut,
select Properties, and adjust
the Start in box:
R.M. Ripley (University of Oxford) R 2012/13 4 / 17
What is R
Starting and Stopping R
Customize as desired using Edit/Gui preferences
To save any
changes, click Save
and accept the
default file name.
Then quit using q()
and restart R.
R.M. Ripley (University of Oxford) R 2012/13 5 / 17
What is R
Multiple windows or one?
R on Windows can use multiple free windows or keep them all inside
one large one (default).
To get multiple
windows either
add --sdi after
the target in the
properties of the
short cut
or choose sdi
via the Edit/Gui
preferences
menu option
R.M. Ripley (University of Oxford) R 2012/13 6 / 17
What is R
Exiting from R
The Esc key will terminate the current operation (Ctrl-C on Linux)
q() will exit from R
Exit via the File menu
Close the window
R.M. Ripley (University of Oxford) R 2012/13 7 / 17
What is R
Help
Type ?help
Type ??variance or help.search("variance") for a subject
help(command) or ?command if you know the command name
?‘&&‘ (or, now, ?"&&") for operators or words such as if
help.start() for R online documentation
Help menu in Windows gives still more options
R.M. Ripley (University of Oxford) R 2012/13 8 / 17
What is R
A brief demo
Type 2 + 3 <return>
Type x <- 2 + 3 <return> (stores result in object called x)
Type exp(-4 * 4 / 2) / sqrt(2 * pi)<return>
Type dnorm(0, 4, 1)<return>
Look for an easy way!
R.M. Ripley (University of Oxford) R 2012/13 9 / 17
What is R
A brief demo
Type 2 + 3 <return>
Type x <- 2 + 3 <return> (stores result in object called x)
Type exp(-4 * 4 / 2) / sqrt(2 * pi)<return>
Type dnorm(0, 4, 1)<return>
Look for an easy way!
R.M. Ripley (University of Oxford) R 2012/13 9 / 17
What is R
A brief demo
Type 2 + 3 <return>
Type x <- 2 + 3 <return> (stores result in object called x)
Type exp(-4 * 4 / 2) / sqrt(2 * pi)<return>
Type dnorm(0, 4, 1)<return>
Look for an easy way!
R.M. Ripley (University of Oxford) R 2012/13 9 / 17
What is R
Introductory session
Now try a (not very!) short introductory session:
Load the package MASS.
Find the location of your MASS library: type searchpaths()
Use the File/Open Script... menu option or otherwise, navigate to
your MASS library folder, then into the scripts folder and open
script file ch01.R
Follow the commands in this file from 1.4 An introductory
session and examine the results.
See section 1.3 of Venables and Ripley, Modern Applied Statistics
with S, 4th edition, for a commentary.
You will learn more if you type the commands into R rather than
copy and paste.
R.M. Ripley (University of Oxford) R 2012/13 10 / 17
Saving and Restoring
Saving and Restoring
All objects are held in memory and not saved unless you request
this.
When you quit (q()) R will ask if you want to save your work.
If you say yes, R writes a file called .RData in the current folder.
When you start R, if there is a file called .RData in the folder, R
loads this.
It is (almost) never a good idea to save the workspace. (Trust me!)
If you use the Mac R.app, automatic loading and saving may work
differently.
R.M. Ripley (University of Oxford) R 2012/13 11 / 17
Saving and Restoring
Specific Saving and Restoring
To save objects, x and y, in a file called xy.RData type
save(x, y, file = "xy.RData")
To save your workspace, type
save.image() or use menu option File/Save Workspace.
R will prompt for a file name.
To load a file you saved earlier called "xy.RData", type
load("xy.RData")
It is a good idea to save data that you have spent a lot of time on,
or model fits that take a lot of time. Much better to save the
workspace yourself with a “real” name than use the default saving
system.
R.M. Ripley (University of Oxford) R 2012/13 12 / 17
Working with R
Working with R
It is a good idea to store commands in one of
1 script window
2 emacs editor
3 other external editor e.g. Tinn-R, WinEdt
Benefits of external editors
Syntax highlighting and checking
Not affected by a crash of R (rare but possible)
Auto-save protection against crashes of computer.
Note that some of Tinn-R’s features require use of SDI interface.
Always use comments (marked by #) and spaces (to improve
readability) liberally.
R.M. Ripley (University of Oxford) R 2012/13 13 / 17
Working with R
Installing R on your own machine
Visit CRAN at http://cran.r-project.org/
Find the appropriate binary
Windows: here.
Mac: select as appropriate
Linux: select as appropriate.
After installation, create a shortcut which starts in your folder
(using right-click/Properties)
Update packages via the packages menu or using
update.packages()
R.M. Ripley (University of Oxford) R 2012/13 14 / 17
Working with R
R may look different for you
Parts of R may appear in other languages than English:
Messages
Menus on Windows and Macs
Only for some messages, as it is not totally automatic,
You will be able to use all the characters in your language as part
of variable names. But not very portable!
If you want to change it, (on Windows), use LANGUAGE=ln in your
short-cut target, where ln is the (ISO-639) code for the desired
language.
LANGUAGE=en to get English.
R.M. Ripley (University of Oxford) R 2012/13 15 / 17
Working with R
More about packages
Many of R’s facilities are provided in packages.
Some are installed with R, others can be downloaded and
installed via the Packages/Install packages option or by using
install.packages()
In order to use a package, it needs to be loaded.
Either type library("package-name") or use
Packages/Load package menu option
e.g. library("car")
R.M. Ripley (University of Oxford) R 2012/13 16 / 17
Exercises
Exercises 1
Explore a package
Install and load the package ‘car’
Read about the dataset Davis.
Use the function scatterplot to plot reported weight (y) against
weight (x) for the subjects in this dataset.
What do the dotted lines represent?
Add a suitable title to the graph.
Random numbers
Generate a sample of size 100 from a Poisson Distribution with
mean 4.
Calculate the standard deviation.
R.M. Ripley (University of Oxford) R 2012/13 17 / 17