## code to accompany Lecture 2 of ## Statistical computing with R by ## Robert B. Gramacy ## so the code will wrap appropriately in the slides options(width=45) ## example expressions x <- 1 if(1 > 2) "yes" else "no" 127 %% 10 ## separating by semicolon "an expression"; 7+13; exp(0+1i*pi) ## symbols NaN <- 1 ## try/not in slides pi pi <- 1 pi ## redfining the combine function try/not in slides c <- 1 c v <- c(1,2,3) v ## resolution at evaluation x <- 1; y <- 2; z <- 3 v <- c(x, y, z) v x <- 10 v ## delayed evaluation x <- 1 v <- quote(c(x, y, z)) eval(v) x <- 5 eval(v) ## promise delayed evaluation x <- 1 delayedAssign("v", c(x, y, z)) x <- 5 v ## everything expression is a function animals <- c("cow", "chicken", "pig", "tuba") animals[4] <- "duck" `[<-`(animals, 4, "duck") if(1 < 2) "this" else "that" `if`(1 < 2, "this", "that") ## immutable/copy u <- list(1) v <- u u[[1]] <- "hat" u v ## immutation witin functions f <- function(x, i) { x[i] <- 4 } w <- c(10, 11, 12, 13) f(w, 1) w ## Special values ## NA v <- c(1,2,3) length(v) <- 4 v ## Inf and -Inf 2^1024 -2^1024 ## NaN Inf - Inf 0 / 0 ## coersion x <- 1:5 x class(x) x[4] <- "hat" x class(x) ## assignments y <- list(shoes="loafers", hat="ball cap", shirt="white") z <- function(a, b, c) { a^b/c } v <- 1:8 ## function LHS assignments dim(v) <- c(2,4) v[2,2] <- 10 formals(z) <- alist(a=1, b=2, c=3) z ## parentheses 2 * (5 + 1) f <- function(x) x 2 * f(5 + 1) ## curly braces for functions f <- function() { x <- 1; y <- 2; x + y } f() ## or outside functions { x <- 1; y <- 2; x + y } ## functions not curly braces create new environments f <- function() { u <- 1; v <- 2; u + v} f() u { u <- 1; v <-2; u + v } u ## if statement examples if(FALSE) "not printed" if(FALSE) "not printed" else "printed" ## try/not in slides if(is(x, "numeric")) x/2 else print("x not numeric") ## vector-valued conditions x <- 10 y <- c(8, 10, 12, 3, 17) if(x < y) x else y ## ifelse a <- rep("a", 5) b <- rep("b", 5) tf <- (1:5) %% 2 > 0 ifelse(tf, a, b) ## if switch switcheroo.if.then <- function(x) { if(x == "a") "alligator" else if(x == "b") "bear" else if(x == "c") "camel" else "moose" } switcheroo.if.then("b") ## switch switch switcheroo.switch <- function(x) { switch(x, a="alligator", b="bear", c="camel", "moose") } switcheroo.switch("b") ## repeat loop i <- 5 repeat { if(i > 25) break else { print(i); i <- i + 5 } } ## while loop i <- 5 while(i <= 25) { print(i) i <- i + 5 } ## for loop for(i in seq(5, 25, by=5)) print(i) ## two special things about loops ## no printing for(i in seq(5, 25, by=5)) i ## scope i <- 1 for(i in seq(5, 25, by=5)) i i ## negative indexing v <- 100:119 v v[-15:-1] l <- list(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10) l ## try/not in slides l[-7:-1] ## subset coersion a <- array(data=101:124, dim=c(2,3,4)) class(a) class(a[1,,]) class(a[1,1,]) class(a[1:2,1:2,1:2]) class(a[1,1,1, drop=FALSE]) ## replacement via brackets a[1,,] a[1,1:2,1:2] <- 1:4 a[1,,] ## indexing lists by name l[c("a", "b", "c")] dairy <- list(milk="1 gallon", butter="1 pound", eggs=12) dairy\$milk dairy[["milk"]] diary[["mil"]] dairy[["mil", exact=FALSE]] ## double list indexing fruit <- list(apples=6, oranges=3, bananas=10) shopping.list <- list(dairy=dairy, fruit=fruit) shopping.list ## try/not in slides shopping.list[[c("dairy", "milk")]] shopping.list[[c(1,2)]] ## factors ecc <- c("brown", "blue", "blue", "green", "brown", "brown", "brown") ecc ## try/not in slides ecf <- factor(ecc) ecf levels(ecf) ## try/not in slides ecf <- factor(ecc, levels=c("green", "brown", "blue"), ordered=TRUE) ecf unclass(ecf) as.integer(ecf) class(unclass(ecf)) ## try/not in slides ## trees data frame and analysis trees pairs(trees) corr(trees) ## try/not in slides ## row and column names colnames(trees) names(trees) rownames(trees)[1:6] ## a formula encoding a model formula(Volume ~ Height + Girth + I(Girth^2)) lm(Volume ~ Height + Girth + I(Girth^2), data=trees) coef(lm(Volume ~ . + I(Girth^2), data=trees)) ## try/not in slides coef(lm(Volume ~ . + .^2, data=trees)) coef(lm(Volume ~ Girth*Height, data=trees)) coef(lm(Volume ~ . + Girth:Height, data=trees)) coef(lm(Volume ~ . + I(Girth*Height), data=trees)) ## time series turkey <- c(131.7, 322.6, 285.6, 105.7, 80.4, 285.1, 347.8, 68.9, 293.3, 375.9, 415.9, 65.8, 177.0, 483.3, 463.2, 136.0, 192.2, 442.8, 509.6, 201.2, 196.0, 478.6, 688.6, 259.8, 352.5, 508.1, 701.5, 325.6, 305.9, 422.2, 771.0, 329.3, 384.0, 472.0, 852.0) turkey turkey <- ts(turkey, start=1979, freq=4) turkey plot(turkey) ## objects in environment objects() rm(x) objects() ## global enviroment to empty x <- .GlobalEnv while(environmentName(x) != environmentName(emptyenv())) { print(environmentName(parent.env(x))) x <- parent.env(x) } ## functions create new environments env.demo <- function(a, b, c, d) { print(objects()) } env.demo(1, "truck", 1:5, pi) ## quick lm/formula environment demo x <- 1:10 y <- 5 + x + rnorm(10) fit <- lm(y~x) lm(y~x, data=data.frame(y=y, x=x)) ## try/not in slides ## with and within df <- data.frame(a=1, b=2, c=3) d <- with(df, a+b+c) print(d) df2 <-within(df, d <- a+b+c) df2 ## attach df attach(df) print(c(a,b,c))