Which R command creates a 2 by 2 matrix with the values 1,2,3 and 4?
Answers
R has no direct way to create an arbitrary matrix. You have to first list all the entries of the matrix as a single vector (an m by n matrix will need a vector of length mn) and then fold the vector into a matrix.
To create this matrix:
So first, we list the entries column by column to get the matrix, as follows:
A = matrix(c(1,3,2,4),nrow=2)
The nrow=2 command tells R that the matrix has 2 rows (then R can compute the number of columns by dividing the length of the vector by nrow.)
R has no direct way to create an arbitrary matrix.
You have to first list all the entries of the matrix as a single vector (an m by n matrix will need a vector of length mn) and then fold the vector into a matrix.
To create this matrix:
So first, we list the entries column by column to get the matrix, as follows:
A = matrix(c(1,3,2,4),nrow=2)
The nrow=2 command tells R that the matrix has 2 rows (then R can compute the number of columns by dividing the length of the vector by nrow.)