Here are some features of R that you may need to do Assignment 3. I've kept the stuff that was relevant for assignment 2 as well. source("file") Read function definitions (or any R commands) from the given file, executing them just as if you had typed them in. NA The special R value meaning "Not Available". Useful to mark things as empty or not yet known. c(x,y,z) Creates a vector with three elements x, y, z (or however many are given). Actually, x, y, and z can themselves be vectors, in which case the result is their concatenation. For example, v <- c(v,12) will append the number 12 to the end of the vector v. You can create a NULL vector (which acts like one with zero elements) using c(). a:b Creates a vector of integers from a to b. rep(e,n) Creates a vector of length n in which all the elements are e. v[i] Extracts the i'th element of the vector v. NOTE: Subscripts start with 1 in R, not 0. for (i in a:b) { ... } Does the statements ... as many times as there are integers from a to b, with i set to each in turn. WARNING: You need parentheses for ranges like 1:(n+1) in order to get the operator precedence right. while (condition) { ... } Does the statements ... as long as the given condition is true. break Breaks out of the enclosing for or while loop. matrix(initial,rows,columns) Creates a matrix with the indicated number of rows and columns, with entries set to the initial value given. mat[i,j] Extracts the value in the i'th row and j'th column of the matrix mat. mat[i,] Extracts the entire i'th row of the matrix mat (as a vector). mat[,j] Extracts the entire j'th column of the matrix mat (as a vector). list (a=x, b=y, c=z) Creates a list with items called "a", "b", "c" having values x, y, z. lis$f Extracts the item called "f" from the list stored in the variable lis. sample(vector,n,replace=TRUE) or sample(vector,n,replace=FALSE) Randomly picks n items from the vector given, returning the picked items as a vector. If replace=TRUE, the same item may be picked more than once. If replace=FALSE, the items picked are all from different positions in the vector. NOTE: A language design "bug" is that R treats a single element vector (ie, just a number) for the first argument as a abbreviation for sampling from the vector of integers from 1 up to that number. This was stupid, but we're stuck with it. runif(1) Returns a random number uniformly drawn from the interval from 0 to 1. set.seed(s) Sets the random number seed to s. Calling set.seed with the same seed will result in subsequent random number generation (eg, with "sample") to produce the same values as it did the last time after setting the seed to s. Useful for making your results reproducible (eg, to help debugging). plot(v) Produces a plot of the values in the given data vector. The plot has 1, 2, 3, ... on the horizontal axis and v[1], v[2], v[3], ... on the vertical axis. There is one point plotted for each element of the vector. hist(data) Plots a histogram from the data (a vector or matrix). par(mfrow=c(1,2)) Sets things up so that two plots appear on each page (saves paper). sum(data) Computes the sum of the data (a vector or matrix). mean(data) Computes the sample mean of the data (a vector or matrix). var(data) Computes the sample variance of the data (a vector or matrix). sd(data) Computes the sample standard deviation of the data (a vector or matrix). min(vector) or max(vector) Find the minimum (or maximum) of the elements in the vector given. any(b) Returns TRUE if any of the logical values in the vector b are TRUE. Eg, any(v<5) will return TRUE if any of the values in the vector v are less than 5. all(b) Returns TRUE if all of the logical values in the vector b are TRUE. cat(a,b,c) Prints out a, b, and c, which may be either numbers or strings. To end a line, print out the "\n" character. For example: cat("The square root of two is",sqrt(2),"\n") prints The square root of two is 1.414214 round(v,d) Rounds the value v to have d digits to the right of the decimal point. abs(x) Returns the absolute value of x. apply(mat,1,fun) apply(mat,2,fun) Returns a vector found by applying the function fun to the rows of the matrix mat (second argument 1) or the columns of matrix mat (second argument 2). For example: > mat <- matrix(NA,3,2) > mat[,1]<-1:3 > mat[,2]<-10:12 > mat [,1] [,2] [1,] 1 10 [2,] 2 11 [3,] 3 12 > apply(mat,1,sum) [1] 11 13 15 > apply(mat,2,sum) [1] 6 33