# Chapter 1 Script # November 15, 2007 # Alan T. Arnholt options(width=65) # par(ask=TRUE) example(plot) par(ask=FALSE) # library(MASS) head(quine) data() ?Cars93 # Sec 1.4.2 (7*3)+12/2-7^2+sqrt(4) x <- 0.28354 round(x,2) x <- c(1.5,2,3) x x^2 which(x^2==4) y <- c("A","table","book") y x <- c(2,3,4,1) y <- c(1,1,3,7) cbind(x,y) rbind(x,y) # Sec 1.4.3 seq(0,1,0.2) seq(0,8) 0:8 8:0 rep(1,5) # rep(c(0,"x"), 3) rep(c(1,3,2), length=10) c(rep(1,3), rep(2,3), rep(3,3)) rep(1:3, rep(3,3)) # typos <- c(2,2,2,3,3,0,3,4,6,4) typos typos[4] typos[3:6] typos[c(3,6,10)] typos[-c(2,3)] # x <- c(1,2,3) names(x) <- c("A","B","C") x names(x) <- NULL x # library(PASWR) Bodyfat$sex[3:6] # Only for R site <-"http://www1.appstate.edu/~arnholta/PASWR/CD/data/Baberuth.txt" Baberuth <- read.table(file=url(site),header=TRUE) Baberuth[1:5,1:9] # First five rows and nine columns # 1.4.6 with(Bodyfat, fat) with(Bodyfat, fat < 25) with(Bodyfat, fat < 25 | fat > 35) low.fat <- with(Bodyfat, fat[fat < 25]) low.fat with(Bodyfat, fat[fat < 25 & fat != 7.8]) # attach(Bodyfat) fat < 25 | fat > 35 # low.fat <- fat[fat < 25] low.fat # fat[fat < 25 & fat != 7.8] detach(Bodyfat) # x <- c(1,6,9,2,NA) is.na(x) y <- x[!is.na(x)] y # x <- c(19,14,15,17,20,23,19,19,21) treatment <- c(rep("A",3), rep("B",3), rep("C",3)) x[treatment=="A"] x[treatment=="A" | treatment=="B"] split(x, treatment) # # Section 1.4.7 Data <- c(190,8,22.0,191,4,1.7,223,80,2.0) barley.data <- matrix(Data, nrow=3, byrow=TRUE) barley.data dim(barley.data) province <- c("Navarra", "Zaragoza", "Madrid") type <- c("typeA", "typeB", "typeC") dimnames(barley.data) <- list(province, NULL) barley.data dimnames(barley.data) <- list(NULL, type) barley.data dimnames(barley.data) <- list(province, type) barley.data dimnames(barley.data) # # dimnames(barley.data) <- NULL # barley.data barley.data[2,] # or barley.data["Zaragoza",] barley.data[,"typeC"] # typeD <- c(2,3.5,2.75) barley.data <- cbind(barley.data, typeD) rm("typeD") barley.data apply(barley.data, 2, mean) apply(barley.data, 1, mean) # x <- c(1,2,3) names(x) <- c("A","B","C") x names(x) <- NULL x # # Section 1.4.8 A <- matrix(c(3,2,1,2,-3,1,1,1,1), byrow=TRUE, nrow=3) A b <- matrix(c(10,-1,6), byrow=TRUE, nrow=3) b x <- solve(A, b) x # A%*%x # Section 1.4.9 cube <- 1:27 dim(cube) <- c(3,3,3) cube[2,2,2] cube[ ,2,2] # a <- array(1:27,dim=(c(3,3,3))) a[, , 1] a[, , 2] a[, , 3] # # Section 1.4.10 student <- list(first.name="John",last.name="Smith",major="Biology", semester.hours=15) student[[4]] student$semester.hours student <- list(first.name="John",last.name="Smith",major="Biology", semester.hours=15,schedule=array(1:3,dim=(c(3,1)))) student$schedule[2,1] student[[5]][2,1] # # Section 1.4.11 cont.weather<-c("no","no","yes") city <- data.frame(barley.data, cont.weather) rm("cont.weather") city # city$typeA attach(city) typeA detach(city) typeC attach(city) city[sort.list(city[,3]),] city[order(city[,3]),] city[order(typeC),] # # Section 1.4.12 library(MASS) attach(Cars93) table(Origin, AirBags) table(Origin, AirBags, DriveTrain) ftable(Origin, AirBags, DriveTrain) CT <- table(Origin, AirBags) CT margin.table(CT) margin.table(CT,1) margin.table(CT,2) prop.table(CT) prop.table(CT,1) prop.table(CT,2) # # Section 1.4.13 tapply(Price, list(Origin, AirBags), mean) aggregate(Price, list(Origin, AirBags), mean) # attach(Baberuth) apply(Baberuth[,3:14], 2, mean) # x <- c(19,14,15,17,20,23,19,19,21,18) treatment <- c(rep("A",5),rep("B",5)) treatment # or treatment <- rep(LETTERS[1:2],rep(5,2)) treatment # tapply(x, treatment, mean) # # Section 1.5 set.seed(136) set1 <- rbinom(10,10,.3) set.seed(136) set2 <- rbinom(10,10,.3) rbind(set1, set2) # # Section 1.6 SUM.N <- function(n){(n)*(n+1)/2} SUM.N(10) # sum.sq<-function(x) {sum(x^2)} apply(barley.data, 2, sum.sq) # # Section 1.7 sum.a <- 0 for (i in c(10,20,30)){sum.a <- i + sum.a} sum.a # for (farenheit in seq(60,90,5)) print(c(farenheit,(farenheit-32)*5/9)) # i <- 0; a <- 0; n <- 50 while (i