################################################### ### chunk number 1: vectors ################################################### vector("numeric", 10) numeric(10) vector("logical", 0) ################################################### ### chunk number 2: pvectors ################################################### rep(1:2, 3) ################################################### ### chunk number 3: pvectors2 ################################################### rep(c("A", "B"), c(2,3)) rep(c("A", "B"), each = 3) ################################################### ### chunk number 4: arrays ################################################### x = 1:10 dim(x) = c(2,5) x ################################################### ### chunk number 5: arrays2 ################################################### x= matrix(1:10, nrow=2) ################################################### ### chunk number 6: names ################################################### x=c(10, 20) names(x) = c("First", "Second") x ################################################### ### chunk number 7: arrayex ################################################### x= array(1:8, dim=c(2,2,2)) dimnames(x) = list(c("A", "B"), NULL, c("X", "Y")) x ################################################### ### chunk number 8: posind ################################################### x= 1:10 x[1:3] ################################################### ### chunk number 9: posind2 ################################################### x[9:11] ################################################### ### chunk number 10: posind3 ################################################### x[0:1] ################################################### ### chunk number 11: posind4 ################################################### x[c(1,2,NA)] ################################################### ### chunk number 12: posassign ################################################### x[1:3] <- 10 x ################################################### ### chunk number 13: negsub ################################################### x[-(1:3)] ################################################### ### chunk number 14: negassignb ################################################### x=1:10 x[-(2:4)] = 10 x ################################################### ### chunk number 15: logicalsub ################################################### x=1:10 x[x>5] ################################################### ### chunk number 16: namedsub ################################################### x= 1:10 names(x) = LETTERS[1:10] x[c("A", "D", "F")] ################################################### ### chunk number 17: arraysub ################################################### x = matrix(1:9, ncol=3) x[x>6] x[row(x) > col(x)] = 0 x ################################################### ### chunk number 18: mode ################################################### mode(1:10) storage.mode(1:10) mode("a string") mode(TRUE) ################################################### ### chunk number 19: mode ################################################### 1 + TRUE ################################################### ### chunk number 20: modeNA ################################################### mode(NA) ################################################### ### chunk number 21: modeNA2 ################################################### 1+NA mode(1 + NA) ################################################### ### chunk number 22: charNA ################################################### is.na(as.character(NA)) ################################################### ### chunk number 23: coerce1 ################################################### as.numeric(c("1", "10.5", "text")) ################################################### ### chunk number 24: coerce2 ################################################### x= 1:5 names(x) = LETTERS[1:5] as.character(x) ################################################### ### chunk number 25: sm ################################################### x= 1:5 names(x) = LETTERS[11:15] x storage.mode(x) = "character" x ################################################### ### chunk number 26: vec1 ################################################### 1:3 + 10:12 ################################################### ### chunk number 27: vec2 ################################################### 1 + 1:5 paste(1:5, "A", sep="") ################################################### ### chunk number 28: listenv ################################################### lst = list(a=1:3, b = "a list") lst ################################################### ### chunk number 29: env1 ################################################### e1 = new.env(hash=TRUE) assign("a", 1:3, e1) assign("b", "a list", e1) ls(e1) ################################################### ### chunk number 30: ################################################### lst[1] ################################################### ### chunk number 31: ################################################### lst[[1]] ################################################### ### chunk number 32: ################################################### lst$a lst[["a"]] ################################################### ### chunk number 33: ################################################### e1$a e1[["b"]] ################################################### ### chunk number 34: ################################################### lst[[1]] = list(2,3) lst[[1]] e1$b = 1:10 e1$b ################################################### ### chunk number 35: loopEx ################################################### for(i in 1:5) if (i == 3) next else print(i) for(i in 1:4) if(i == 3) break else print(i) ################################################### ### chunk number 36: ################################################### sum = 0 x = rnorm(10000) system.time({ s = 0 for(i in 1:length(x) ) s= s+ x[i]}) system.time({s=0 for(v in x) s = s + v}) ################################################### ### chunk number 37: sqfun ################################################### square = function(x) x*x square(10) ################################################### ### chunk number 38: sf2 ################################################### square(1:4) ################################################### ### chunk number 39: callsumsq ################################################### sumsq = function(x) sum(square(x)) sumsq(1:10) ################################################### ### chunk number 40: fac1 ################################################### facI = function(n) { ans = 1 for(i in seq(n)) ans = ans*i ans } facI(5) ################################################### ### chunk number 41: fac2 ################################################### facR = function(n) if ( n <= 0 ) 1 else n * facR(n - 1) facR(5) ################################################### ### chunk number 42: facV ################################################### facV = function(n) prod(seq(n)) facV(5) ################################################### ### chunk number 43: facG ################################################### facG = function(n) gamma(n+1) facG(5) ################################################### ### chunk number 44: hypot ################################################### hypot = function(a, b) sqrt(a^2 + b^2) ################################################### ### chunk number 45: sumsq ################################################### sumsq = function(x, about = 0) sum(( x-about)^2) ################################################### ### chunk number 46: ss2 ################################################### sumsq = function(x, about = mean(x)) sum((x-about)^2) ################################################### ### chunk number 47: funnyfun ################################################### silly = function(a=b, b=a) a+b ################################################### ### chunk number 48: ss2 ################################################### sumsq = function(x, about = mean(x)) { x = x[!is.na(x)] sum((x-about)^2) } ################################################### ### chunk number 49: oddreturn ################################################### f = function(x) { z = g(x, return(3)) print("in f") z } g = function(n, y) if(n < 5 ) y else 4 f(3) f(10) ################################################### ### chunk number 50: onexit ################################################### f = function(x) { on.exit(print("hi")) 5 } f ################################################### ### chunk number 51: subs ################################################### g = function(x) substitute(x) g(x[1]+y*2) ################################################### ### chunk number 52: depsubs ################################################### g = function(x) deparse(substitute(x)) g(x[1]+y*2) ################################################### ### chunk number 53: subs1 ################################################### g <- function(a, b) substitute(a+b) g(x*x, y*y) ################################################### ### chunk number 54: sub3 ################################################### g = function(...) substitute(list(...)) g(a=10, b=11) ################################################### ### chunk number 55: frag1 ################################################### x = 10 y = 20 f = function(y) { x+y } ################################################### ### chunk number 56: binom ################################################### choose = function(n, k) { fac = function(n) if(n <= 1) 1 else n*fac(n-1) fac(n) / (fac(k) * fac(n-k)) } ################################################### ### chunk number 57: gausslike ################################################### mkNegLogLik = function(x) { function(mu, sigma) { sum(sigma + 0.5 * ((x-mu)/sigma)^2) } } q = mkNegLogLik(rnorm(100)) ################################################### ### chunk number 58: methods ################################################### mean methods("mean") ################################################### ### chunk number 59: S4 ################################################### setClass("foo", representation(a="numeric")) b = new("foo", a=10) b b@a