Notes from Computing Club -- 3 November 2006 Everything you wanted to know about R and were afraid to ask... with Rafa and Roger [NB: these are the chicken-scratch notes jotted down during the session. There may be mistakes and things that are not fully complete in them. The main idea was to help you find that elusive function that you remember hearing about but don't remember what it was called or quite how to use it. If you find a glaring error in these notes, let us know. -NR & HW] ~ Cntl-L clears the screen ~ using along looping through a length of a list 0 for ( i in seq(along=x)) print("1") ~ rowMeans(), colMeans() are better than using apply( , fun=mean) ~ can apply be run to always output the same class? as in, sometimes apply will return a matrix, othertimes it returns a list. solution: lapply always returns a list. foo=function(x) 1:(sample(1:10,1)) x=matrix(rnorm(100*10),100,10) note: tapply returns function based on factors ~ defining a function or variable within a loop that is dependent on the iteration # CALLING FUNCTIONS f1=mean f2=var f3=median x=rnorm(100) for (i in 1:3) { funname = paste(f, i, sep="") do.call(funname, list(x=x)) } # ASSIGNING VARIABLE NAMES for (i in 1:3) { varname=paste("x", i, sep"") tmp = rnorm(100) assign(varname, tmp) } #USING LISTS sx=vector("list", 5) for (i in 1:3) { xs[[i]] = rnorm(100) } ~ summmarize an object in words str() ~ remove all objects from the workspace rm(list=ls()) ~ removing object with a pattern x1=2 x2=3 y=4 rm(list=ls(pattern="x")) ~returns the regular expression that corresponds to the unix "glob" expression e.g. glob2rx("x*") ~reverse search cntl-R multiple times (doesn't work on windows) ~ side-by-side boxplots x is data y is factor boxplot(x~y) boxplot(split(x,y)) ~ data() # shows available datasets ~ writing tables FOR LATEX write.table(airquality, file="aq.tex", sep="&", eol="\\\\\n") \\\\\n because we want a "\\[end of line]" FOR EXCEL write.csv(airquality, file="aq.csv") ~debug() must undebug() unless you redefine the function ~ finding modulus (e.g. 4%%2) ~ eval() eval(mean(rnorm(1000)) eval(parse(text="mean(rnorm(1000)))) ~ WITH FUNCTION (data is a dataframe, col1, col2 are vectors of the columns) with(data, col1+col2) ~ .Rprofile file (in home directory -- maybe different for windows) can have anything that you want to run whenever you open R also, a Rprofile in the directory that you are opening something in and it will run that too. ~ file.path("as", "asd") returns the system-dependent file path ~ pressing tab twice shows a list of files ~ system.time() # returns the time it took to run something ~ object.size(x) # returns size of the object ~ match() finds the indices of elements of x in y x=1:4 y=sample(1:10) match(x,y) ~ x %in% y (returns T/F of whether elements in x are in y) ~ split(x,y) stratifies x on y. ~ for colors plot(x,y,col=as.numeric(as.factor(z))) ~ colors() shows available colors library RColorBrewer brewer.pal(9, "Blues") display.brewer.all() shows you all pallets