Homework Problem 1, STAT 705, Fall 2015. Assigned 8/31/2015, Due 9/9/2015 (a) Give the R code lines to create a single long integer vector in your workspace, named A1, consisting of 528 concatenated entries as follows: 1,2,...,10 appearing 3 times each, followed by 17,18,...,26 appearing 5 times each, followed by 51,...,58 appearing 12 times each; and then this whole vector repeated twice more, once in the same order and once in reversed order. (b) Give the R code lines to create a real numeric vector in your R workspace named A2 of length 250 consisting of sin(1+2*x/7) for index x=1,2,...,250. (c) Create a character vector A3 of labels "B", "C", "D", "E" where B appears at every index at which the A2 value falls in [-0.4, 0.5], C appears at every index where the A2 value is greater than 0.8 in absolute value, D appears whenever A2 is in (-0.8,-0.4), and E appears whenever A2 is in (0.5,0.8). How many entires are there of each type ? (d) From A3, create a numeric vector A4 of the same length as A2 which has entry 2 wherever the A3 entry is "B", 3 wherever the A3 entry is "C" or "D", and 5 wherever the A3 entry is "E". (e) Define A5 as a list with components named "B","CD","E" which are respectively the numeric vectors of indices at which A4 has respective values 2, 3, and 5. (f) Give the R code lines to create a matrix defined by binding together the first 250 elements of the vector A1 with the vectors A2 and A3 with column names "Symb", "Num", "Char", "Gp" respectively. What is the "class" -- i.e., data-type -- of your matrix ? What is the class of columns 2 and 3 of the matrix ? (g) Give the R code lines to create a data-frame of dimension 250 by 4 out of the same columns A1 (first 250 entries), A2, A3 and A4 with the same column names as in (f). But in this part, make sure that columns 1, 2 and 4 of the resulting data-frame remain numeric, while column 3 remains of "character" type. (h) Print out lines 20, 50, 80, 110, 140, 170, 200, 230 of the data-frame you create in (g). YOU ARE RESPONSIBLE FOR CHECKING and "showing" THAT THE R CODE YOU HAND IN DOES EXACTLY WHAT IT IS SUPPOSED TO, BUT DO NOT HAND IN THE PRINTED-OUT FORMS OF ALL OF THE VECTORS AND MATRIX AND COMPLETE DATA-FRAME YOU CREATE ! YOU MAY IF YOU LIKE HAND IN SOME INTERMEDIATE RESULTS, PERHAPS (A SMALL) PART OF THE VECTORS, MATRICES, OR DATA-FRAMES YOU CREATE TO DOCUMENT THAT YOU HAVE CODED YOUR COMMANDS CORRECTLY. BUT KEEP THIS PART ALSO AS BRIEF AS POSSIBLE: EACH EXTRA ITEM YOU INCLUDE SHOULD HAVE A SPECIFIC JUSTIFICATION AND INTERPRETATION AS A CHECK ON YOUR WORK ! INCLUDE SOME WORDS ### IN COMMENT LINES LIKE THIS WITHIN THE CHECKING PORTIONS OF YOUR HANDED-IN PAPER TO EXPLAIN WHAT YOU ARE DOING. ##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > T1 = c(rep(1:10, rep(3,10)), rep(17:26, rep(5,10)), rep(51:58,rep(12,8))) A1 = c(T1,T1,rev(T1)) > table(T1) T1 1 2 3 4 5 6 7 8 9 10 17 18 19 20 21 22 23 24 25 26 51 52 53 54 55 56 57 58 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 12 12 12 12 12 12 12 12 > A2 = sin(1+(2/7)*(1:250)) A3 = ifelse( abs(A2-0.05)<=.45, "B", "C" ) A3[abs(A2+.6)<0.2] = "D" A3[abs(A2-0.65)<0.15] = "E" > table(A3) A3 B C D E 74 114 39 23 > A4 = 1 + as.numeric(factor(A3)) A4[A4==4] = 3 table(A4) A4 2 3 5 74 153 23 > A5 = list( B = which(A4==2), CD = which(A4==3 | A4==4), E = which(A4==5) ) > unlist(lapply(A5,length)) B CD E 74 153 23 > mat1 = cbind(Symb=A1[1:250], Num=A2, Char=A3, Gp=A4) mat1[1:10,] Symb Num Char Gp [1,] "1" "0.959638582966685" "C" "3" [2,] "1" "0.999999800133368" "C" "3" [3,] "1" "0.95928219572884" "C" "3" [4,] "2" "0.84078710579525" "C" "3" [5,] "2" "0.654121974506596" "E" "5" [6,] "2" "0.414421393761095" "B" "2" [7,] "3" "0.141120008059867" "B" "2" [8,] "3" "-0.143623223881818" "B" "2" [9,] "3" "-0.416721651753499" "D" "4" [10,] "4" "-0.656032772424843" "D" "4" > class(mat1) [1] "matrix" c(class(mat1[,2]), class(mat1[,3])) [1] "character" "character" > fr1 = cbind.data.frame(Symb=A1[1:250], Num=A2, Char=A3, Gp=A4) > unlist(lapply(fr1,class)) Symb Num Char Gp "integer" "numeric" "factor" "numeric" > fr1$Char = as.character(fr1$Char) unlist(lapply(fr1,class)) Symb Num Char Gp "integer" "numeric" "character" "numeric" > fr1[seq(20,230,by=30),] Symb Num Char Gp 20 7 0.4178708 B 2 50 20 0.4098129 B 2 80 26 -0.9567446 C 3 110 53 0.8482356 C 3 140 55 -0.1586227 B 2 170 58 -0.6396585 D 4 200 8 0.9997264 C 3 230 21 -0.6749080 D 4