############# # Section 7 ############# help(Arithmetic) help(Math) #Then click methods and scroll to Math help(Syntax) help(+) help("+") #Math functions sqrt(3:10) abs(-3:3) sin(90) sin(90*pi/180) exp(-3:3) log(-3:3) factorial(8) choose(6,2) # Operations on vectors myvec <- (1:4)*pi myvec myvec*myvec myvec+myvec myvec^2 2*myvec^2 (2*myvec)^2 t(myvec) * myvec t(myvec) %*% myvec set.seed(34) # just so you will get the same results as I do: mymat <- matrix(rnorm(16), nc=4) solve(mymat,myvec) solve(mymat) solve(mymat) %*% mymat round(solve(mymat) %*% mymat, digits=0) nrow(mymat) ncol(mymat) dim(mymat) newmat <- rbind(myvec-1, 2*myvec) newmat2 <- cbind(myvec-1, 2*myvec) newmat %*% newmat2 cbind(newmat, 2*newmat) myvec as.matrix(myvec) #Arithmetic operations on an entire matrix a=matrix(1:25,ncol=5) a a^2 #Matrices vs. vectors myvec=1:10 dim(myvec) colvec=as.matrix(myvec) attributes(colvec) tcolvec=t(myvec) dim(tcolvec) tcolvec set.seed(21) # just so you will get the same results as I do: unif.vec <- round(runif(25, min=10, max=20),1) a<-matrix(unif.vec,nrow=5,ncol=5) #More matrix operations diag(10) b=1:5 solve(a,b) a%*%solve(a,b) b solve(a) a%*%solve(a) round(a%*%solve(a),1) rbind(a,b) cbind(a,b)