###introduction to R#################### ##R package can be downloaded from website for free http://www.r-project.org## ##use "help" to know the functions## help(sqrt) #r will tell you the usage of sqrt function and give you exmaples how to use the function ## or use help.search("***") ## Using a '#' comments out any text afterwards on a line #########Random Number Generators#################################### ##Seed ##A pseudo random number generator is an algorithm based on a starting point called "seed". ##If you want to perform an exact replication of your program, you have to specify the seed ##using the function set.seed(). The argument of set.seed has to be an integer. set.seed(1) runif(10) x <- rnorm(10,mean=0,sd=1) y <- rnorm(n=10) ## default generates N(0,1) ## plotting. plot(x,y) ##Sampling in a vector ##Toss 10 coins sample(0:1,10,replace=T) #Roll 10 dice sample(1:6,10,replace=T) ##play lottery (6 random numbers out of 49 without replacement) sample(1:49,6,replace=F) ##replace=F is the default ##select a simple random sample a<-c(1,3,5,7,9,11,13,15,17,19) b<-sample(1:10,5) b a[b] #or srs.sample<-a[sample(1:10, 5, replace=F)] ##illustrate simple random sampling (from Yihui XIE) x = cbind(rep(1:10, 10), gl(10, 10)) par(mar = rep(0.1, 4)) for (i in 1:10) { plot(x, pch = 19, col = "blue", axes = F, ann = F) points(x[sample(100, 15), ], col = "red", cex = 3, lwd = 2) Sys.sleep(1) } ###### Matrix Algebra ######### ######### Vectors ########### ## Concatenate function 'c' to create a vector u <- c(1,2,3,4) ## print u print(u) ##just typing u also prints u u <- 1:4 ## makes the same vector as above u v <- seq(0,1.5,by=.5) v v <- seq(0,1.5,length=4) ## same thing w <- rep(u, times=2) w ## Scalar mult 2*u ## Vector Addition u+v ## square every element of u u^2 ## multiply each element of u by the corresponding elements of v u*v ## sum up every element of u sum(u) ## subsetting v[1] ## get the first element of v v[1:3] ## get the first 3 elements of v v[c(2,4)] ## get the 2nd and 4th elements of v v[-4] ## get all but the 4th element of v ######### Matrices ########## A <- rbind(u,v) A B <- cbind(u,v) B ## subsetting A[1,1] ## get the (1,1) element of A A[1:2,1:3] ## get the first 3 rows of A with the first three columns A[,1:2] ## get the 1st and 2nd columns of A (with all rows) B[-3,] ## get all but the 3rd row of B (with all columns) ## (e.g. eliminate he 3rd observation) ## transpose t(A) ## Matrix multiplication C <- A%*%B C A%*%C ## doesn't work b.c. A is (2x4) and C is (2x2) C%*%A ## does work ## Element by element multiplication C*C ## is not the same as C%*%C C%*%C A*C ## doesn't work, A and C must have the same dimensions ## Matrix Inverse C.inv <- solve(C) C.inv C%*%C.inv ## Note: C^(-1) does NOT work C%*%C^(-1) ## does not equal the identity #########read data#################################### ## Data frames are basically the same as matrices ## Turn a matrix B into a data frame B.data <- as.data.frame(B) ## read data into a data frame from a webpage ex.data <- read.table(file="http://www.stat.unm.edu/**/**.txt", header=T) ex.data ## Read data from a local file ex.data <- read.table(file="W:/teaching/stat445545anova/CH16PR07.txt") ex.data