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]: yWhen 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.
help(BATCH)in R for more information or click here.
> help(log) > ?logto 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:
> 456 + 783 > 2389 / 23 > 34 ^ 56 > log(756) > sqrt(72)
> 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
> 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
> ls() [1] "var1" "x" "y" > rm(x)
> 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)
> A %*% B
> sqrt(-3) > [1] NaN > Warning message: > NaNs produced in: sqrt(-3)
> x <- c(-1,3,2,-4,5,6)Now create a new vector.
> i <- x<0This 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
> i <- x<0> x[i]This returns the vector
[1,] -1,-4A 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]
> 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.
> 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 variableOther distributions are chisq, f, t, binom,poisson, etc. The corresponding functions for the t distribution are rt, pt, qt, dt .
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
> standardizeNote 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
> 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.
> 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
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.
> X11() # open a graphics device in R> dev.off() # close a graphic device in RNote 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)
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
> plot(1:10)
> dev.print(postscript,file="graph1.ps")
The file, graph1.ps, is saved in the directory in which R is invoked. Other graphics devices (i.e. x11, jpeg, etc.) can be specified. Type
> help(Devices)for a list of the available devices. When the graph is saved to an appropriate file, it can then be printed.
> plot(1:10)
You can save/print a graph in two ways.
Save asunder the File drop down menu. This is shown below.
To copy the graph to the clipboard as a bitmap or metafile, left click on
Copy to the clipboardunder the File drop down menu. This is shown below. Note that this is equivalent to right clicking on the graph and selecting one of the
Copy as metafileor
Copy as bitmapoptions. Once the graph is copied to the clipboard, the graph is pasted into an editor such as MS Word.
Note that you can print directly to a printer as well.
To quit R, do the following:
> q() > Save workspace image? [y/n/c]: yWhen 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.