# ---------------------------------------------------------------------------- # R_examples.R # Various examples to show how things work # General reference: http://cran.r-project.org/manuals.html # Written by Dave Backus, April 2011 and after, based on numerous # conversations with the talented Espen Henriksen. # Many of the examples adapted from R's online help and other online sources. # ---------------------------------------------------------------------------- setwd("c:/Documents and Settings/dbackus/My Documents/Userdata/Papers/BCH/data/R_examples") getwd() # check directory (matters only for input/output) # 0. Arithmetic noquote("set x = 3") x <- 3 noquote("print x (two versions)") x print(x) noquote("mess around with x") y <- 2*x + 17 y sqrt(x) noquote("print pi") pi noquote("vectors") x <- c(1,3,7,4,9,2) x noquote("operations on x") sort(x) mean(x) sd(x) # ------------------------------------------------------------------------------ # 1. Matrix elements # this works better if you type the commands yourself # note that vectors are printed as rows m <- matrix(1:12,3,4) noquote("matrix") m noquote("dimensions of m") dim(m) noquote("fifth element (order runs down rows)") m[5] noquote("element (3,1)") m[3,1] noquote("last element") m[length(m)] noquote("third column") m[,3] noquote("third row") m[3,] noquote("double brackets(??)") m[[5]] # obscure in this context noquote("kill second column") m_not2 <- m[,-2] m_not2 rm(m_not2) noquote("choose cols(1,2,2,4)") m_new <- m[,c(1,2,2,4)] m_new noquote("or this way...") cols <- c(1,2,2,4) m[,cols] rm(m_new) noquote("subset of m") m[1:2,3:4] noquote("subsets based on logical comparisons") m[m >= 3] # ------------------------------------------------------------------------------ # 2. Data frames # illustrate their structure, how to use col/row names, etc noquote("built-in data frame") mtcars help(mtcars) noquote("just the start and end") head(mtcars) tail(mycars) noquote("dimensions") ncol(mtcars) nrow(mtcars) noquote("names of variables and observations") names(mtcars) rownames(mtcars) colnames(mycars) noquote("first column (three versions, note differences)") mtcars[1] mtcars[,1] mtcars[,"mpg"] noquote("first row") mtcars[1,] noquote("first column (just data)") mtcars[[1]] noquote("first column (variable reference)") mtcars$mpg noquote("the (3,2) element") mtcars[3,2] noquote("create new column/variable") mtcars$mpgsq = mtcars$mpg^2 head(mtcars) noquote("if you attach variable names...") attach(mtcars) noquote("...you can use variable names on their own") mpg noquote("logical indexing (pick cars w mpg >= 15)") i = mtcars$mpg >= 15 i mtcars[i,] # ------------------------------------------------------------------------------ # 3. Data input (barely started) # see: http://cran.r-project.org/doc/manuals/R-data.html # read.table is the basic, read.csv and read.xls are similar # read.table and write.table # useful comment from R FAQ: 7.28 Why is read.table() so inefficient? # By default, read.table() needs to read in everything as character data, and then try to figure out which variables to convert to numerics or factors. For a large data set, this takes considerable amounts of time and memory. Performance can substantially be improved by using the colClasses argument to specify the classes to be assumed for the columns of the table. # ditto csv # ditto xls : but add these libraries library(gtools) library(gdata) # ------------------------------------------------------------------------------ # 4. Graphics (barely started) # See: http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/00Index.html setwd(directory) # Example A: sin function x <- 1:30 y <- sin(x)*0.92 # various options # par() for list of options, ? par for help # type="l" for line # main=title, ylab=y-axis, xlab=x-axis # ylim, xlim axis limits # cex=expansion factor for making characters bigger # mar = margins (bottom, clockwise) # mgp: first controls y-axis label margin, second controls axis labels, # third separates ticks from axes {plot(x,y,type="l",cex=1.5,lwd=1.5, main="", xlab="", ylab="0.92*sin(x)", ylim=c(-1.1,1.1), mar=c(4,4,2,2), # better than default in most cases mgp=c(2.5,1,0) )} # make lines ("box") around plot thicker box(bty="o", lwd=4, col="red") # excessive, but makes the point # add text text(24,-0.9,"XYZ",pos=4) # pos=4 means left justified # margin text mtext("Source: R example", side=1, line=2.5, cex=.6, adj=0) # test print options # second one works best for me in LaTeX dev.print(device=postscript, file="R_examples_1.eps",horizontal=FALSE,width=8,height=6) dev.copy2eps(device=postscript, file="R_examples_2.eps",width=8,height=6) dev.print(device=pdf, file="R_examples.pdf",width=8,height=6) # Example B plot(mtcars$mpg,mtcars$cyl) # Example with multiple y axes x <- 1:5 y1 <- rnorm(5) y2 <- rnorm(5,20) par(mar=c(5,4,4,5)+.1) plot(x,y1,type="l",col="red") par(new=TRUE) # odd word choice, but it's like Matlab's "hold on" plot(x, y2,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="") axis(4) mtext("y2",side=4,line=3) legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2"))