Write your homework using R Markdown and submit it in HTML format via Canvas. Name your homework as “HW#_LastName_FirstName”, where “#” is the homework number (1,2,…). In order to receive credits, you must include the necessary R codes, outputs and textual explanations in your submitted work.
a1=c(5,"7",9,12,"15") and
a2=c(5,7,9,12,15). What are their types? Hint: use the
functions typeof(), is.numeric() or
is.character().## [1] "character"
## [1] FALSE
## [1] TRUE
## [1] "double"
## [1] TRUE
## [1] FALSE
b. Run the codes `a1[1]+3` and `a2[1]+3`. Explain the results.
## [1] 8
c. Run the codes `a1==a2`, `identical(a1,a2)` and `all.equal(a1,a2)`. Explain the results.
## [1] TRUE TRUE TRUE TRUE TRUE
## [1] FALSE
## [1] "Modes: character, numeric"
## [2] "target is character, current is numeric"
d. Create a new vector `a3=as.numeric(a1)`. What is the type of `a3`? Run the codes `a3==a2`, `identical(a3,a2)` and `all.equal(a3,a2)`, and see what happens now.
## [1] "double"
## [1] TRUE TRUE TRUE TRUE TRUE
## [1] TRUE
## [1] TRUE
e. Run the code `a2[3]="9"`. What happens to `a2`? What is the type of `a2` now? Try `a2[1]+3` again.
## [1] "character"
1:5, which gives back a vector containing the numbers 1
through 5. This is a special case of the function seq().
Using the help command ?seq to learn about the function,
design an expression using seq()that will give you the
equivalent of (1:10)*2. Also design an expression to create
the sequence of numbers from 1 to 200 in increments of 15. Design
another that will give you a sequence between 1 and 200 that includes
exactly 18 numbers in length.## [1] 2 4 6 8 10 12 14 16 18 20
## [1] 1 16 31 46 61 76 91 106 121 136 151 166 181 196
## [1] 1.00000 12.70588 24.41176 36.11765 47.82353 59.52941 71.23529
## [8] 82.94118 94.64706 106.35294 118.05882 129.76471 141.47059 153.17647
## [15] 164.88235 176.58824 188.29412 200.00000
b. The function `rep()` repeats a vector some number of times. Explain the differences or similarities of `rep(1:10, times=5)`, `rep(1:10, each=5)` and `rep(1:10, length.out=50)`.
## [1] 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5
## [26] 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
## [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5
## [26] 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10
## [1] 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5
## [26] 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
seq() and
matrix().## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 3 7 11 15 19
c(2,4,6,8,10) to the matrix above and name
the new matrix. Hint: use the function rbind()## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 3 7 11 15 19
## [3,] 2 4 6 8 10
colSums()## [1] 6 16 26 36 46
rowSums()## [1] 45 55 30
rep(1,5)## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 3 7 11 15 19
## [3,] 2 4 6 8 10
is.matrix() and
is.vector() to check.## [1] FALSE
## [1] TRUE
## [1] TRUE
## [1] FALSE