Homework Solutions to Problems 1--2. =================================== [NB solutions are in Splus 6.0] (1) > V1 <- rep(c(0,1,1),40) V2 <- sin(2*pi*sqrt(1:120)) ### A better version (used below) would be: ### V2 <- round(sin(2*pi*sqrt(1:120)),1e-12) V3 <- ifelse(V2>=0, "H", "L") newmat <- cbind(Group = V1, Y = V2, Type = V3) newfram <- cbind.data.frame(Group = V1, Y = V2, Type = V3, stringsAsFactors=F) ### Note that the "stringsAsFactors=F" optional argument is ### needed to avoid automatic conversion of character data ### to factor-type. > unlist(lapply(newfram,class)) Group Y Type "integer" "numeric" "character" > newfram[1:5,] Group Y Type 1 0 0 H 2 1 1 H 3 1 -1 L 4 0 0 H 5 1 1 H > newmat[1:5,] Group Y Type [1,] "0" "0" "H" [2,] "1" "1" "H" [3,] "1" "-1" "L" [4,] "0" "0" "H" [5,] "1" "1" "H" (2) > exmpfram <- read.table(file="exampfram.txt", stringsAsFactors=F, skip=1) > dim(exmpfram) [1] 216 13 > exmpfram <- exmpfram[,3:9] > names(exmpfram) <- c("IDNUM","DTH","EVTTIME","TRTGP","LOGBILI", "AGEVAR","CIRRH") > exmpfram$CIRRH <- exmpfram$CIRRH == "T" > class(exmpfram$CIRRH) [1] "logical" > exmpfram2 <- scan(file="exampfram.txt", skip=1) ### This is a giant vector, of type "character" and length 216*13=2808 ### We are interested in pulling out the elements 3:8 in each row, ### which now correspond to elements with indices 3:8 plus ### multiples 0:215 of 13. > inds <- rep(3:9,216) + rep(13*(0:215),rep(7,216)) inds2 <- c(outer(3:9, 13*(0:215), "+")) > sum(abs(inds-inds2)) [1] 0 > exmpfram2 <- data.frame(matrix(exmpfram2[inds], ncol=7, byrow=T, dimnames=list(NULL, names(exmpfram))), stringsAsFactors=F) > exmpfram2[,7] <- as.logical(exmpfram2[,7]) > for(i in c(1:3,5:6)) exmpfram2[,i] <- as.numeric(exmpfram2[,i]) > unlist(lapply(exmpfram2,class)) IDNUM DTH EVTTIME TRTGP LOGBILI AGEVAR CIRRH "numeric" "numeric" "numeric" "character" "numeric" "numeric" "logical" > c(sum(exmpfram == exmpfram2), sum(unlist(lapply(exmpfram2,class))== unlist(lapply(exmpfram,class)))) [1] 1512 7 ### solves part (c), since all 216*7 entries match (including type) > t(apply(exmpfram[,c(1:3,5:6)],2,range)) ### solution to part (d) [,1] [,2] IDNUM 1.00000 248.00000 DTH 0.00000 1.00000 EVTTIME 0.00000 139.44000 LOGBILI 0.69897 2.72263 AGEVAR 1.64900 330.30000 ### same matrix is produced by: > cbind(apply(exmpfram[,c(1:3,5:6)],2,min), apply(exmpfram[,c(1:3,5:6)],2,max))