This preview shows pages 1–4. Sign up to view the full content.
Introduction to R
R is a high level language especially designed for statistical calculations. R is free.
You can get it at:
http://www.cran.rproject.org/
There are versions for Unix, Linux, Windows and Mac. There is a similar program
called Splus. The commands in the two languages are virtually identical. Splus has more
stuF in it but R is free and it is faster. If you want to use Splus, you can purchase a copy
from Insightful at http://www.splus.mathsoft.com/.
1
Getting Started
In Unix or Linux, you start R by typing:
R
. In windows, click on the R icon. You can
now use R interactively. Just start typing commands.
You can also use R in Batch mode. To do this, store your R commands in a ±le, say,
±le.r. In R type:
source("file.r")
which will execute the commands in ±le.r. In Unix
(or Linux), you can also do the following:
which will execute the commands and store them in ±le.out.
NOTE:
Use the command: q() to quit from R.
Use
help(xxxx)
to get help on command xxxx. Better yet, type
help.start()
to
open up a help window.
2
Basics
Here is a simple R session. The # symbol means “comment.” R ignores any command
after #. I have added lots of comments below to explain what is going on. You do not
need to type the comments.
x = 5
### assign x the value 5
x
### print x
print(x)
### another way to print x
x < 5
### you can also use < to make assignments
y = "Hello there"
y
y = sqrt(10)
z = x + y
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document z
q()
### Use this to quit
Scalars are treated by Splus as vectors of length 1. That is why they print with a
leading “[1]” indicating that we are at the Frst element of a vector.
Vectors can be created using the
c()
command.
c()
stands for concatenate. Square
brackets are used to get subsets of a vector. The colon is used for sequences. Start up R
again then do this:
x = 1:5
### the vector (1,2,3,4,5)
print(x)
x = seq(1,5,length=5)
### same thing
print(x)
x = seq(0,10,length=101) ### 0.0, 0.1, .
.., 10.0
print(x)
x = 1:5
x[1] = 17
print(x)
x[1] = 1
x[3:5] = 0
print(x)
w = x[3]
### everything except the third element of x
print(w)
y = c(1,5,2,4,7)
y
y[2]
y[3]
y[c(1,4,5)]
i = (1:3)
z = c(9,10,11)
y[i] = z
print(y)
y = y^2
print(y)
y = 1:10
y = log(y)
y
y = exp(y)
y
x = c(5,4,3,2,1,5,4,3,2,1)
z = x + y
z
### R carries out operations on
2
### vectors, element by element.
If you add vectors of diFerent lengths then R automatically repeats the smaller vector
to make it bigger. This generates a warning if the length of the smaller vector is not the
same length as the longer vector.
x = 1
y = 1:10
x + y
x = 1:3
y = 1:4
x + y
x = 1:10
y = c(5,4,3,2,1,5,4,3,2,1)
x == 2
### This is a logical vector.
z = (x == 2)
print(z)
z = (x<5); print(z)
### You can put two commands
### on a line if you use a semicolon.
x[x<5] = y[x<5]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 06/06/2011 for the course STAT 4360 taught by Professor Park during the Spring '11 term at University of Georgia Athens.
 Spring '11
 Park

Click to edit the document details