Complete the code to create a 3x3 matrix named mat filled with numbers 1 to 9.
mat <- matrix(1:9, nrow = [1], ncol = 3)
The nrow argument sets the number of rows. To make a 3x3 matrix, nrow must be 3.
Complete the code to get the number of rows of matrix mat.
num_rows <- nrow([1])matrix or data which are not the matrix variable.The function nrow() returns the number of rows of the matrix passed as argument. Here, the matrix is mat.
Fix the error in the code to get the number of columns of matrix mat.
num_cols <- [1](mat)nrow() which returns rows, not columns.length() which returns total elements.The function ncol() returns the number of columns of a matrix. Using nrow() or length() will not give the correct number of columns.
Fill both blanks to create a 2x4 matrix named m filled with numbers 1 to 8.
m <- matrix(1:8, nrow = [1], ncol = [2])
To create a 2x4 matrix, nrow must be 2 and ncol must be 4.
Fill all three blanks to create a matrix mat with 3 rows, 2 columns, and get its dimensions.
mat <- matrix(1:6, nrow = [1], ncol = [2]) dims <- [3](mat)
ncol instead of dim to get dimensions.Set nrow = 3 and ncol = 2 to create a 3x2 matrix. Use dim() to get both dimensions as a vector.