Lab Guide — An Introduction to R
The aim of this lab guide is to introduce the statistical computing language
R
. Initially we
simply use
R
as a clever calculator, and later gently introduce the key components of writing
your own programs.
Contents
1
Getting Started with
R
2
2
Using
R
as a Calculator
5
2.1
Scalars
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
2.2
Vectors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
2.3
Functions and Operations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
2.4
Integer Arithmetic
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
2.5
Logical Comparisons
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
2.6
Matrices
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
2.7
Lists
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
3
Practice Problems
25
A Managing Your Work Environment
33
1
An Introduction to R

1
Getting Started with
R
R
is a freely available programming language and environment for statistical computing and
graphics which provides a wide variety of statistical and graphical techniques: linear and non-
linear modelling, statistical tests, time series analysis, classification, clustering, etc.
You can
download
1
it and use it from home. We use
R
in this course because it knows alot about proba-
bility (e.g., pdfs and cdfs of many distributions), makes it very easy to use vectors and matrices,
and is easier to learn than other computer programming languages you may have heard of like
C++ or Java.
The computers in the School of Mathematics, Statistics and Operations Research (MSOR)
student computing laboratories use a Unix operating environment.
This exercise is meant to
help you take the first and most difficult steps: successfully getting on the computer, connecting
to the Unix system, running R, and subsequently reversing these steps for the first time.
Note.
The first five steps of this exercise apply to the Unix operating system on the computers in
the MSOR computing labs. If you are doing this exercise on a Windows or Mac-OS environment,
then assuming you have R installed, you start R by double-clicking on the left mouse button,
as you would with any other application. You can then proceed to step 6 below.
Step 1.
Enter the statistical computing laboratory CO535 and locate an available workstation.
It will normally show a blank screen or the login window.
Step 2.
Move the mouse to terminate the screensaver and redisplay the login window (the
screen may take a few seconds to become bright again), then type your MSOR username
(followed by a
Return
) and password (followed by a
Return
) at the keyboard.
Your MSOR username is in general the same as your VUW SCS login name, but you
must set this up at MSOR once before you can use it at MSOR. Follow the instructions in
the document
"Computer Account Registration"
which is a coloured A4 notice found
on the door of each MSOR computer laboratory. [To summarise, you use the username
register
and the password
register
to initiate the activation process, then follow the
instructions on the screen.]
Step 3.
If the login proceeds successfully, the screen will show a panel of icons at the bottom.