Recall & Review
beginner
What function in R is used to find the dimensions of a matrix?
The
dim() function returns the number of rows and columns of a matrix as a vector.Click to reveal answer
beginner
How do you create a 3x2 matrix in R?
Use
matrix(data, nrow = 3, ncol = 2). For example, matrix(1:6, nrow = 3, ncol = 2) creates a matrix with 3 rows and 2 columns filled with numbers 1 to 6.Click to reveal answer
intermediate
What does
dim() return if applied to a vector in R?It returns
NULL because vectors do not have dimensions like matrices do.Click to reveal answer
intermediate
How can you change the dimensions of a vector to make it a matrix?
Assign a dimension attribute using
dim(vector) <- c(rows, columns). For example, dim(v) <- c(2,3) turns vector v into a 2x3 matrix.Click to reveal answer
beginner
What is the difference between
nrow() and ncol() functions in R?nrow() returns the number of rows of a matrix, while ncol() returns the number of columns.Click to reveal answer
What does
dim(matrix(1:6, nrow=2, ncol=3)) return?✗ Incorrect
dim() returns the number of rows and columns. Here, 2 rows and 3 columns.If
v <- 1:6, what does dim(v) return?✗ Incorrect
Vectors do not have dimensions, so
dim() returns NULL.How do you convert a vector
v of length 6 into a 2x3 matrix?✗ Incorrect
Assigning
dim(v) <- c(2,3) changes the vector into a 2-row, 3-column matrix.What does
ncol(matrix(1:4, nrow=2)) return?✗ Incorrect
ncol() returns the number of columns. Here, the matrix has 2 columns.Which function gives the number of rows in a matrix?
✗ Incorrect
nrow() returns the number of rows in a matrix.Explain how to find and change the dimensions of a matrix in R.
Think about how you check size and how you can turn a vector into a matrix.
You got /3 concepts.
Describe the difference between a vector and a matrix in terms of dimensions in R.
Consider what dim() returns for each.
You got /3 concepts.