Chapter 2

Matrices

Is it true that this is a vector?

matrix(1)

No,

because matrix(1) is two-dimensional.


Is it true that this is a vector?

     [,1]
[1,]    1

No,

because this is the result of matrix(1) and matrix(1) is two-dimensional.


How many dimensions does m have when m is

matrix(1,1)

two

because m is a matrix.


How many dimensions does m have when m is

matrix(1,2)

two

because m is a matrix.


How many dimensions does m have when m is

matrix(1,2,3)

two

because m is a matrix.


How many rows does m have when m is

matrix(1,2,3)

two

because the second argument in m is 2.


How many columns does m have when m is

matrix(1,2,3)

three

because the third argument in m is 3.


What is dim(m) when m is

matrix(1,2,3)

2 3

because dim() is the same as asking the values of the dimensions of a matrix.


How many values does m have when m is

matrix(1,2,3)

six

because two rows times three columns is six.


What is sum(m) where m is

matrix(1,2,3)

six

because there are six values in m and each value is equal to one.


What is sum(m) where m is

matrix(2,2,3)

twelve

because there are six values in m and each value is equal to two.


What is length(m) where m is

matrix(2,2,3)

six

because there are six values in m.


How many columns does m have where m is

matrix(15,1,15)

and

dim(m) <- c(3,5)

5

because m was altered to have five columns by dim.


How many columns does m have where m is

matrix(20,4,5)

and

dim(m) <- c(2,20)

5

because the m is 20, but the number of values produced by dim(m) <- c(2,20) is 40.


What is m[1,3] where m is

matrix(c(1,2,3),3,3)

1

because matrix(c(1,2,3),3,3) is

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3

and the value in the first row, third column is 1.


What is m[1,3] where m is

matrix(c(1,2,3),2,3)

2

because matrix(c(1,2,3),2,3) is

     [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3

and the value in the first row, third column is 2.


What is the name of the second column of m when m is

matrix(c(1,2,3),3,3)

and

colnames(m) <- c("April", "May", "June")

"May"

because "May" is the second value given to colnames.


What is m[1,3] where m is

matrix(1:6,2,3)

5

because matrix(1:6,2,3) is

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

and the value in the first row, third column is 5.


What is m[1,3] where m is

matrix(1:6,3,3)

1

because matrix(1:6,3,3) is

     [,1] [,2] [,3]
[1,]    1    4    1
[2,]    2    5    2
[3,]    3    6    3

and the value in the first row, third column is 1.


What is m[1,2] where m is

c(1,2,3,4)

and

dim(m) <- c(2,2)

3

because m is now

     [,1] [,2]
[1,]    1    3
[2,]    2    4

and the value in the first row, second column is 3.


What is class(m[1,2]) where m is

c(1,2,3,4)

and

dim(m) <- c(2,2)

integer

because the matrix was made out of an integer vector.


What is m[2,2] where m is

list(1,"2",3,"4")

and

dim(m) <- c(2,2)
[[1]]
[1] "4"

because m is now

     [,1] [,2]
[1,] 1    3   
[2,] "2"  "4" 

What is class(m[2,2]) where m is

list(1,"2",3,"4")

and

dim(m) <- c(2,2)

list

because the matrix was made out of a list.