############################################## ############################################## ### ### Answers of exercises ### Chapter 8 and 9 ### ##### Ex. 1 test <- 481 test2 <- "this is a test" objects() ls() objects(pattern="s*") ##### Ex. 2 x1 <- c(1,3.5,0.7,5) data.class(x1) x2 <- as.integer(x1) x3 <- round(x1) is.integer(x3) ##### Ex. 3 y1 <- 1:10 y2 <- y1 > 5 sum(y2) ##### Ex. 4 y1 <- c("high", "low", "high", "high", "low") y2 <- as.factor(y1) levels(y2) ## first transform y2 to an ordered factor y2 <- ordered(y2,levels=c("low","high")) y3 <- as.integer(y2) y3 ##### Ex. 5 x <- 1:100 y <- seq(0,500,by=5) or y <- (0:100)*5 z1 <- rep(1:50,each=3) or z1 <- rep(1:50, rep(3,50)) z2 <- rep(1:10,1:10) tmp <- rep(c(1,2),25) z3 <- rep(1:50,tmp) ##### Ex. 6 x <- rnorm(100) y <- cut(x,breaks=c(-Inf,-1.5,1.5,Inf),labels=c("low","average","high")) ## y is still a numeric vector y <- as.factor(y) ##### Ex. 7 x <- rnorm(100) test <- x > 1.96 | x < -1.96 (1:100)[test] ##### Ex. 8 tmp <- rnorm(100) mat1 <- matrix(tmp,ncol=10) tmp1 <- 1:10 tmp2 <- 10:1 mat1 <- rbind(tmp1,mat1,tmp2) tmp1 <- 1:12 mat1 <- cbind(mat1,tmp1) ##### Ex. 9 tmp1 <- rnorm(1000) tmp2 <- 1:1000 tmp3 <- rep(1:10,100) testdf <- data.frame(Values=tmp1, number=tmp2,Group=tmp3) ##### Ex. 10 vec <- rnorm(10) testlist <- list(vec=vec,df=testdf) res <- rnorm(10) testlist2 <- list(res=res,tl=testlist) names(testlist2) testlist2$tl$df$Group ##### Ex. 11 x <- 1:98 tmp <- (1:14)*7 y <- rep(tmp,each=2) ## subtract 1 from elements of tmp which have an odd subscript. oddsubscripts <- (1:14)*2 -1 y[oddsubscripts] <- y[oddsubscripts]-1 y tmp <- match(x,y) x <- x[tmp=="NA"] x ### The dates function start to count at zero and x starts at 1 so subtract 1 from x dates(x-1,origin=c(m=6,d=5,y=2000)) ## or use the timeDate function timeDate(julian=x-1,in.origin=c(month=6,day=5,year=2000)) ##### Ex. 12 test <- car.test.frame$Price > 9500 cars1 <- car.test.frame[test,] test <- car.test.frame$Price > 9500 & car.test.frame$Type == "Sporty" & car.test.frame$Country != "Japan" cars2 <- car.test.frame[test,] tmp <- row.names(car.test.frame) test <- regexpr("[cv]",tmp) test <- test > 0 cars3 <- car.test.frame[test,] ##### Ex. 13 x <- 1:20 y <- runif(20) plot(x,y) title("My Plot") ymax <- max(y) xc <- x[y==ymax] text(xc,ymax,"highest y value") plot(x,y,xlab="day number", ylab="stock price") lines(x,y) ##### Ex. 14 x <- seq(-2*pi,2*pi,l=100) y1 <- sin(x) y2 <- cos(x) y3 <- tan(x) plot(x,y1,type="l") lines(x,y2,lty=2) lines(x,y3,lty=3) ## or set the y range larger, because the tan function has a wider y range plot(x,y1,type="l",ylim=c(-8,8)) lines(x,y2,lty=2) lines(x,y3,lty=3) ##### Ex. 15 pairs(air) hist(air$temperature,breaks=c(min(air$temperature),65,75,85,max(air$temperature))) ##### Ex. 16 ## zet layout op 2 bij 2 par(mfrow=c(2,2)) t <- seq(0,2*pi,l=100) z1 <- sin(3*t) z2 <- sin(6*t) plot(z1,z2,type="l") z1 <- sin(3*t) z2 <- sin(8*t) plot(z1,z2,type="l") z1 <- sin(3*t) z2 <- sin(11*t) plot(z1,z2,type="l") z1 <- sin(7*t) z2 <- sin(8*t) plot(z1,z2,type="l") ##### Ex. 17 x <- seq(0,2*pi,l=50) y <- x z <- outer(x,y,FUN=function(x,y){sin(x)+cos(y)}) persp(x,y,z) ##### Ex. 18 cars.aov <- aov(Price~Type,data=car.test.frame) summary(cars.aov) mcType <- multicomp(cars.aov) mcType plot(mcType) ##### Ex. 19 ## there are 60 rows in car.test.frame test <- sample(1:60,size=15,replace=F) cars2 <- car.test.frame[test,] ##### Ex. 20 testdf3 <- data.frame(x=rnorm(500),y=rnorm(500),z=rnorm(500)) out1 <- lm(z~x,data=testdf3) out2 <- lm(z~x+y,data=testdf3) anova(out1,out2) st.res <- out2$residuals st.res <- (st.res - mean(st.res))/stdev(st.res) test <- order(st.res) ## case numbers of 5 smallest standardized residuals test[1:5] ## case numbers of 5 largest standardized residuals test[(length(test)-4):length(test)] ##### Ex. 21 cars.lm <- lm(Price ~ Weight + Mileage,data=car.test.frame) newcars <- data.frame(Mileage=c(33,40,43),Weight=c(3000,2500,2800)) predict(cars.lm,newcars) ##### Ex. 22 tmean <- function(x,n){ n1 <- length(x) tmp <- sort(x) mean(tmp[ (1+n) : (n1-n) ]) } tmean(rnorm(100),5) tmean <- function(x,n){ n1 <- length(x) if(n > n1/2){ return("Invalid n") } tmp <- sort(x) mean(tmp[ (1+n) : (n1-n) ]) } tmean(rnorm(100),51) ##### Ex. 23 lissajous <- function(a,b){ t <- seq(0,2*pi,l=1000) z1 <- sin(a*t) z2 <- sin(b*t) plot(z1,z2,type="l") } lissajous(11,13)