###################################################################### # R commands Stat 704 # More Topics in R University of South Carolina ###################################################################### # This file contains the R commands for the lab. # # Lines beginning with the symbol '#' are comments in R. All other # lines contain code. # # In R for Windows, you may wish to open this file from the menu bar # (File:Display file); you can then copy commands into the command # window. (Use the mouse to highlight one or more lines; then # right-click and select "Paste to console".) ###################################################################### ## review ## objects ## for loops ## writing functions ################## export figure pdf(file="/Users/yen-yiho/Desktop/test.pdf") plot(1:10, 1:10, pch=16) dev.off() ################# par op<-par() y<-rnorm(100) par(mfrow=c(1,3)) hist(y, xlab="Standard normal distribution", breaks=seq(from=-4, to=4, by=1), main="Centered Title") par(adj=0) hist(y, xlab="Standard normal distribution", breaks=seq(from=-4, to=4, by=1), main="Left Justified Title") par(adj=1) hist(y, xlab="Standard normal distribution", breaks=seq(from=-4, to=4, by=1), main="Right Justified Title") par(op) ############################## ## if..else ##################### standard<-TRUE m=4 v=8 if (standard) { y<-rnorm(100) hist(y) m1<-mean(y) v1<-var(y) } else { y<-rnorm(100, mean=m, sd=sqrt(v)) hist(y) m1<-mean(y) v1<-var(y) } ############################## ## ifelse ##################### y<-runif(100) trt<-ifelse(y<0.5, 1, 0) trt table(trt) ############################## ## for loops ##################### x<-runif(10) for(i in x){ print(i) } ############################## # for loops & ifelse ############################## for (i in 1:10){ if (i > 5){ print(i) } } ## if .. else x<-rnorm(10, mean=5, sd=1) for (i in x){ if (i < 5){ msg<-paste(i, " is equal or less than 5", sep="") print(msg) } else { msg<-paste(i, " is larger than 5", sep="") print(msg) } } ############################## # CLT ############################## y1<-rep(NA, 100) for(j in 1:length(y1)){ y1[j] <-mean(rnorm(100, mean=3, sd=2)) } y2<-rep(NA, 100) for(j in 1:length(y2)){ y2[j] <-mean(rnorm(10, mean=3, sd=2)) } plot(density(y2), col=2, lty=3, lwd=4, ylim=c(0,2)) lines(density(y1), col=3, lwd=4) hist(y1) var(y1) hist(y2) var(y2) ############################## # apply, sapply, lappy ############################## ### apply ############################## m<-matrix(1:20, nrow=10, ncol=2) ## create a matrix of 10 rows x 2 columns apply(m, 1, mean) ## mean of the rows: rowMeans apply(m,2, mean) ## mean of the columns: colMeans myfun<-function(x){ ans<-x^2 return(ans) } ############################## ###### sapply ############################## x<-1:10 sapply(x, myfun) #####alternative sapply(x, function(x) x^2) #### myid<-paste("ID-", round(runif(10),2)*100, sep="") strsplit("ID-11", split="-") sapply(myid, strsplit, split="-") mystrsplit<-function(x){ ans<-strsplit(x, split="-")[[1]][2] return(ans) } numid<-sapply(myid, mystrsplit) names(numid)<-NULL myid ############################## ###### lapply ############################## x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE)) sapply(x, quantile) A<-matrix(1:9, nrow=3) B<-matrix(4:12, nrow=3) C<-matrix(rep(c(8,9,10), 2), ncol=2) MyList<-list(A,B, C) lapply(MyList,"[", 1, 2) lapply(MyList, "[", , 2) lapply(MyList, "[",1 , ) A B C ############################## ###### Bootstrap ############################## varbs<-function(x, B=200){ bsmean<-rep(0, B) len<-length(x) for(i in 1:B){ bsmean[i]<-mean(sample(x, len, replace=T)) } genmean<-mean(bsmean) bsvar<-sum((bsmean-genmean)^2/(B-1)) return(bsvar) } x<-rnorm(100) varbs(x)