SOLUTION TO HW1, STAT 705 FALL 2017 ## (a) > LongI = NULL for (k in 1:20) LongI = c(LongI, rep(1:k,k)) ## (b) > c(length(LongI), sum((1:20)^2)) [1] 2870 2870 > LongI[c(777,1200)] [1] 10 5 ### entry 777 is the 127th after the 650 entries obtained by repetitions of 1, ..., 1:12, ### then 117 entries later we have the 9th completed cycle 1:13, so the 127th entry is a 10 ### entry 1200 is the 185th after the 1015 obtained be repetitions up to 1:14, then ### 180 entries later we have the 12th completed cycle 1:15, so 5 entries later is a 5. ## (c) > digits = as.numeric(strsplit(paste(LongI,collapse=""),"")[[1]]) > length(digits) [1] 3970 > table(digits) digits 0 1 2 3 4 5 6 7 8 9 185 1445 373 339 323 305 285 263 239 213 > hist(digits, breaks=seq(-0.5,9.5, by=1), plot=F)$count [1] 185 1445 373 339 323 305 285 263 239 213 > nams = c("John","James","Henry","Doug","Richard") > abs(sin(100*(1:10))) [1] 0.50636564 0.87329730 0.99975584 0.85091936 0.46777181 0.04418245 0.54397052 0.89396965 0.99780327 [10] 0.82687954 > nums = substr(as.character(abs(sin(100*(1:10)))),3,5) > CMAT = outer(nams,nums,paste0) CMAT [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] "John506" "John873" "John999" "John850" "John467" "John044" "John543" [2,] "James506" "James873" "James999" "James850" "James467" "James044" "James543" [3,] "Henry506" "Henry873" "Henry999" "Henry850" "Henry467" "Henry044" "Henry543" [4,] "Doug506" "Doug873" "Doug999" "Doug850" "Doug467" "Doug044" "Doug543" [5,] "Richard506" "Richard873" "Richard999" "Richard850" "Richard467" "Richard044" "Richard543" [,8] [,9] [,10] [1,] "John893" "John997" "John826" [2,] "James893" "James997" "James826" [3,] "Henry893" "Henry997" "Henry826" [4,] "Doug893" "Doug997" "Doug826" [5,] "Richard893" "Richard997" "Richard826" ## (e) > LongStr = paste(CMAT,collapse="") > CharVec = strsplit(LongStr,"")[[1]] c(nchar(LongStr), length(CharVec)) [1] 400 400 ##(f) > CombList = list(longI = LongI, tabl = table(digits), matr = CMAT, chv = CharVec) sapply(CombList, class) longI tabl matr chv "integer" "table" "matrix" "character" ### In the case of long vectors, "class" tells the underlying data type ## but a "table" and "matrix" have additional structure that is reflected in their "class".