Keep Matrix Structure when Extract a Row/Column from a Matrix

Posted on Aug 14, 2012 in Programming

Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.

I talked in the post Abuse of Vector in R that it is often the case that we want keep the matrix sturcture when extracting a row/column from a matrix, i.e., we often want to return a matrix with 1 row/column when extracting a row/column from a matrix. By default R returns a vector when you extract a row/column from a matrix. To override this behavior, you can specify the option drop=FALSE when extracting submatrix from matrix. A illustrative example is given below.

a = matrix(1:16,nrow=4)
a[1,,drop=FALSE] -> b
b
[,1] [,2] [,3] [,4]
[1,]    1    5    9   13

On the contrary, if you want to keep the sturcture of a matrix with multiple rows and columns but convert a matrix with 1 row/column to a vector in R, you can the function drop. See the following example.

drop(a)
[,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
drop(b)
[1]  1  5  9 13