Question

Using the following command to create matrix in R, what would be the output?

matrix(c(1:9), nrow=3, ncol=3)
  1. Option A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
  1. Option B
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

Answer

The default behaviour of matrix() function is to fill the matrix by column. So option B is True, to fill the matrix by rows, one has the use byrow argument and set it to TRUE.

matrix(c(1:9), nrow=3, ncol=3, byrow=TRUE)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

Thanks for reading. If you find a correction, hit me up on Twitter and if you like the question, how about a tip: PayPal TipJar