1. Hw and pi.r. 2. mode. 3. R Cookbook. 1. Hw. I changed the wording slightly on problem 3. By "90th largest", I meant the 90th in the sorted list from smallest to largest. It's revised online. e ~ 2.718. Euler's number. Print? Read through ch. 6. pi.r. 2. mode. x = c(17,4.3,2.1,4.3,1) table(x) which.max(table(x)) ## Outputs the index in sorted list of x, not the mode. y = table(x) names(y) z = as.numeric(names(y)) ## equivalent to z = sort(unique(x)) z[which.max(y)] mode2 = function(x){ ## finds only the first mode y = table(x) z = as.numeric(names(y)) z[which.max(y)] } x = c(17,17,4,4,2) mode2(x) mode3 = function(x){ ## finds the mode(s) y = table(x) z = as.numeric(names(y)) y1 = as.numeric(y) w = (y1 == max(y1)) z[w] } ## equivalently, we could replace w = (y1 == max(y1)) with w = which(y1 == max(y1)) mode3(x) x = c(rep(1,7), rep(10,7), rep(-1,8)) mode3(x) x1 = x[x != -1] mode3(x1) 3. R Cookbook. p51, setting the working directory is really important. You need this to read files or write to files and know where they go. p54 search() lists all packages currently loaded [not just installed but loaded into the current session]. search() library(MASS) search() detach(package:MASS) search() p57, datasets. head(pressure) ## or just pressure or pressure[1,] data() ## lists all the preloaded datasets. instead of data(Cars93, package="MASS") you could just do library(MASS) data(Cars93) p58, library() lists all installed (but not necessarily loaded) packages. install.packages() is useful to install a new one. p63, source() is sometimes useful though it can have problems, especially if your comments go over lines, as in the example with ## this will be the total of all the elements in my dataset. Chapter 4, Input and Output. p72, can type straight into R [scores = c(61, 66, 90, 88, 100)] or use the editor [scores = edit(scores)]. ## typo in the text. score should be scores. sink() p 75 is sometimes useful, especially when outputting within a loop. Similar to cat("x", file = "x.txt") read.fwf() p 77 is nice. A similar (and I think better) function is strsplit(). You can load a whole page of numbers, text, etc. using read.delim() and then take the column you want using strsplit(). read.table() is great, p79. scan() p 87 is great, and once in R, you can usually manipulate the data quite easily into a table or matrix anyway. For instance, sink("y.txt") z = runif(102) cat(z) sink() y = scan("y.txt") x = matrix(y, ncol=3, byrow=T) read.csv p81 and readHTMLTable p84 don't work too well sometimes. Same with dbConnect() on p90. The example on p88 and 89 about scan is definitely worth reading, and also illustrates the use of order(). You save perm = order(world.series$year) so that you can keep year and pattern in the same order as each other. Note that for a list, like world.series, you use the $ key to get one element of the list. For instance, x = list(a = c(1:3), b = rep(5,7)) x$a x$b p91 illustrates the useful function paste() x = 1 y = 3.4 z = paste("The answer to problem",x,"is",y,".") cat(z) ## Note that paste adds a space between elements by default. ## You can change sep to "" to change this. z = paste("The answer to problem",x,"is",y,".",sep="") cat(z) You can extract part of a character string using substr(). substr(z,5,10) ## Done with Chapter 4.