R Tutorial


How to start/quit R

You start R by typing "R" at the unix prompt. R is available on the following undergrad machines: bacon, agnesi, blanch, fenchel, fitch, maddison, magnus, merrill and rees. To connect to one of these type "rlogin maddison" and then run R. R is available on the following grad machines: pythagoras and beta.

To quit R, type the following in R:

> q()
> Save workspace image? [y/n/c]: y
When you quit R, you will be asked whether you want to save the workspace image. If you answer "y", all the objects you have created will be saved, and be available next time you start R. If you answer "n", they will be removed.

How to work using R

Basically, you have 3 choices :

Help in R

You can type either of
   > help(log)
   > ?log
to get help on the log command. Type
   > help.start()
to start a help window. This is also a way to list all the R commands.

Note that if you do not know the command in R that you want help on, these different methods of searching R for relevant information concerning a particular subject are (most of the time) not useful. Three other ways of searching for R information are:

Using R as a calculator

You can use R to calculate, eg.
   > 456 + 783 
   > 2389 / 23
   > 34 ^ 56
   > log(756)
   > sqrt(72)

Vectors / Extraction of some elements

   > x 
			<- c(1,3,5,7,8,9)>
			 x
   [1] 1 3 5 7 8 9
   > y 
			<- 1:20>
			 y
   [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
   > z 
			<- seq(1,2,0.1)>
			 z
   [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0
   > x[3]
   [1] 5
   > x[1:3]
   [1] 1 3 5

   > x[-2]
   [1] 1 5 7 8 9

Matrices / Extraction of some elements

   > X 
			<- cbind(x,1:6) # You can also use rbind to combine rows>
			 X
        x  
   [1,] 1 1
   [2,] 3 2
   [3,] 5 3
   [4,] 7 4
   [5,] 8 5
   [6,] 9 6
   > Y 
			<- matrix(0,2,3)>
			 Y
          [,1] [,2] [,3]    
     [1,]    0    0    0
     [2,]    0    0    0
   > Y 
			<- matrix(x,2,3)>
			 Y
          [,1] [,2] [,3] 
     [1,]    1    5    8
     [2,]    3    7    9
   > Y[1,2]  # extract element on the first row and the second column 
   [1] 5
   > Y[1,]   # extract the first row
   [1] 1 5 8 
   > Y[,1]   # extract the first column
   [1] 1 3
   > Y[2,c(1,3)] # extract elements (1,3) of the second row
   [1] 3 9

Listing/Removing objects from your workspace

ls() will list object in your workspace. rm() will remove an object from your workspace.
   > ls()
   [1] "var1" "x"    "y"   
   > rm(x)

Operations on Vectors

One of the quirks/strengths of R is it's use of vectorized operations. Create two vectors x and y, having the same length, and see what happens when you do each of the following operations
   > x + y 
   > x - y
   > x / y  #division element by element
   > x * y  #multiplication element by element
   > sqrt(y)
   > x^3
   > x^y
   > log(x)
   > cos(x)

Matrix multiplication

To multiply two matrices A and B together, use the operator %*% in the following way:
   > A %*% B

The value NA

NA is the value R uses for a missing or undefined value. For instance, try
  
   > sqrt(-3)
   > [1] NaN
   > Warning message:
   > NaNs produced in: sqrt(-3)

Comparison and Logical Operators

Suppose you create a vector, x, in the following way:
   > x 
			<- c(-1,3,2,-4,5,6) 
Now create a new vector.
>
			 i 
			<- x<0 
This returns a vector of logical values
 [1,] (T,F,F,T,F,F) 
The first element of x is less than 0, so the first element of i is T. The second element of x is greater than 0, so the second element of i is F, etc. Other comparison operators are> , <=,> =, == The logical operators are &(and) |(or) !(not)
   > i 
				<- x="=3" | x="=6">
				 i
   [1] F T F F F T
   > i 
				<- x<6 & x>
				3 
   > i
   [1] F F F F T F

Subsetting by Value

Suppose you want to extract a subset of a vector based on its values. For instance, if you want to extract just those elements of x that are less than 0, type
   > i 
				<- x<0>
				 x[i]
This returns the vector
   [1,]  -1,-4 
A short cut is
   > x[x
				<0]>
				 x[x
				<6 & x>
				3]
Say you have two vectors that give the weight and height of 7 people
   > weight 
				<- c(110,120,115,136,205,156,175)>
				 height 
				<- c(64, 67, 62, 60, 77, 70, 66) 
To extract the weight of all people whose height is greater than 65,
>
				 weight[height>65] 

Read a file: read.table("filepath")

The function read.table is very useful for reading data from an external ascii file into R and storing it in a data frame. For example, the following commands
   > data 
				<- read.table("toto",header='T,sep=",")' 
reads the file, "toto", stores it in the variable, "data", gives names to the variables according to the first line of the file (option header), and recognizes the coma sign in the file as being a separator between the fields.

Probability functions

R has a family of probability functions for each of several distributions. For instance, for the normal distribution, there is rnorm, pnorm, qnorm, dnorm. rnorm(10, 1, 2) generates 10 independent values from a normal distribution with mean 1 and standard deviation 2
   > dnorm(1.5, 1, 2) #returns the value of the normal(1,2) pdf at x=1.5
   > pnorm(1.5, 1, 2) #returns the value of the normal(1,2)  cdf at x=1.5
   > qnorm(0.2, 1, 2) #returns x such that P(X
					<=x) 0.2 #where X is a normal(1,2) random variable 
Other distributions are chisq, f, t, binom,poisson, etc. The corresponding functions for the t distribution are rt, pt, qt, dt .

How to write your own functions

R has a lot of built-in functions, but you will soon come upon situation where you want to write one of your own. Here is a simple example.
standardize
						<-function(x) { # Inputs: a vector x # Outputs: the standardized version of x # m<-mean(x) std<-sqrt(var(x)) result<-(x - m)/std return(result) } 
The function takes one argument, a vector x, and returns a vector. The lines beginning with # are comments. The last line tells the function to return the value result, i.e. the original vector x, transformed by subtracting its mean, and then dividing by its standard deviation. To invoke the function on a vector x, type
>
						 xstand 
						<- standardize(x) 
To see the commands which make up the function, just type
>
						 standardize
Note that there are no brackets used. If you want to create a function which returns several outputs, here is a simple example.
sqacu
						<-function(x) { # Inputs: a vector x # Outputs: the square of x and the cube of x # res1<-x^2 res2<-x^3 return(list("square"='res1,"cube"=res2))' } 
Now, if you type
>
						sqacu(2)
   $square:
   [1] 4

   $cube:
   [1] 8
   >sqacu(2)$square
   [1] 4

R functions that are available through add-on packages

Some R functions may not be available when you first enter R and are available using different add-on packages. Refer to the listing of packages given on the R homepage to see these functions. Suppose you want to use the function, survreg. This function is available in the package, survival. To load the package, type
   > library(survival)
You will now be able to use the function, survreg. You can find out which functions a package provides by typing
   > libarary(help=survival)
or
   > help(package=survival)

If you are using R on your own computer, other commands may be needed. Refer to the Section 5 of the FAQ under the R Homepage Documentation.

Iteration

Avoid iteration if you can; take advantage of R vectorized math and functions such as apply. It successively applies the function of your choice to each row (or column) of a matrix. Let's create a simple matrix and use apply to find the mean of each row/column.
   > x 
						<- matrix(1:12,3,4)>
						 apply(x,2,mean) #returns the mean of each column.
   > apply(x,1,mean) #returns the mean of each row

However sometimes iteration cannot be avoided. Then you can use the R commands for or while . Here is an example of using for inside a function
jsum
						<-function(x) { jsum <- 0
        for(i in 1:length(x)) 
        {
                jsum <- jsum + x[i]
        }
        return(jsum)
}
Note that R has its own function that performs this task, called sum. It will work MUCH faster than this one, especially on large vectors.

Graphics

To open/close a graphics device :
   > X11() # open a graphics device in R>
						 dev.off()  # close a graphic device in R
Note that you do not need to open a graphics device to use a plotting function. An X11() graphics device is opened automatically when a plotting function is called. For instance, the plotting function, plot is very useful for plotting some datas. Here's an example:
   > plot(1:10)

Saving/Printing Graphics

Depending on the operating system that you are using, saving/printing graphics in R differs. Simple methods for saving/printing graphs in R under UNIX or Windows are given below. For more detailed information about saving/printing in R, refer to Section 5.2 in the R Tip Sheet.

Saving/Printing Graphics From UNIX

Saving/Printing Graphics From Windows

You can save/print a graph in two ways.

Quitting R

To quit R, do the following:

   > q()
   > Save workspace image? [y/n/c]: y
When you quit R, you will be asked whether you want to save the workspace image. If you answer "y", all the objects you have created will be saved, and be available next time you start R. If you answer "n", they will be removed.

All the objects are saved in a single file called .Rdata. This file will be placed in whatever directory you were when you started R. One way to manage different projects is to have a different directory for each project, and start R in the right directory. Another alternative is to save your objects as text files, and read them in. This means you'll have to keep all your commands stored in a file, which is a good idea anyhow.