Here are some features of R that you may need to do Assignment 2. 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). a:b Creates a vector of integers from a to b. 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. 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. 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). hist(data) Plots a histogram from the data (a vector or matrix). 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). 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. 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