#> Q1 Q2 Q3 Q4
#> 1st "Jan" "Apr" "Jul" "Oct"
#> 2nd "Feb" "May" "Aug" "Nov"
#> 3rd "Mar" "Jun" "Sep" "Dec"
Quiz 5 Instructions
Please complete the following questions and submit a file named Quiz5.R to Gradescope for autograding.
Remember:
- Do not use global paths in you script. Instead, use setwd() interactively in the console, but do not forget to remove or comment out this part of the code before you submit. The directory structure of your machine is not the same as the one on Gradescope’s virtual machines.
- Do not destroy or overwrite any variables in your program. I check them only after I have run your entire program from start to finish.
- Check to make sure you do not have any syntax errors. Code that doesn’t run will get a very bad grade.
- Make sure to name your submission Quiz5.R
Tip: before submitting, it might help to clear all the objects from your workspace, and then source your file before you submit it. This will often uncover bugs.
Question 1
[4pts] Use matrix()
to create a matrix mat1
(see below) from the R built-in constant vector month.abb
. Use the function colnames()
to add the standard calendar quarters that make up the year, that is, add the column names Q1, Q2, Q3, Q4
. Add 1st, 2nd, 3rd
as row names.
Question 2
[3pts] Create a matrix as below and denote it as mat2
.
#> [,1] [,2]
#> [1,] 1 1
#> [2,] 1 2
#> [3,] 1 3
#> [4,] 2 1
#> [5,] 2 2
#> [6,] 2 3
Question 3
Assume the matrix m
is defined as follows. We find that m
is a matrix, not a vector, as shown below.
<- matrix(c(1,2,3,4,5,6), nrow=2)
m is.matrix(m) # is.matrix() tests if the object is a matrix
#> [1] TRUE
is.vector(m) # is.vector() tests if the object is a vector
#> [1] FALSE
- [1pt] Check the dimension of
m
usingdim()
function. Save the result to objectd
.
- [1pt] Define
m1
the same asm
above. Then Remove the dimension attribute ofm1
by usingdim(m1) <- NULL
. Then check ifm1
is a matrix? a vector?
- [1pt] Let
m2 <- c(1,2,3,4,5,6)
. Add the dimension attribute tom2
by usingdim(m2) <- c(2, 3)
. Then check ifm2
is a matrix? a vector?