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.

#>     Q1    Q2    Q3    Q4   
#> 1st "Jan" "Apr" "Jul" "Oct"
#> 2nd "Feb" "May" "Aug" "Nov"
#> 3rd "Mar" "Jun" "Sep" "Dec"

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.

m <- matrix(c(1,2,3,4,5,6), nrow=2)
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
  1. [1pt] Check the dimension of m using dim() function. Save the result to object d.
  1. [1pt] Define m1 the same as m above. Then Remove the dimension attribute of m1 by using dim(m1) <- NULL. Then check if m1 is a matrix? a vector?
  1. [1pt] Let m2 <- c(1,2,3,4,5,6). Add the dimension attribute to m2 by using dim(m2) <- c(2, 3). Then check if m2 is a matrix? a vector?