How to subset matrix to one column, maintain matrix data type, maintain row/column names?

Use the drop=FALSE argument to [.

m <- matrix(1:10,5,2)
rownames(m) <- 1:5
colnames(m) <- 1:2
m[,1]             # vector
m[,1,drop=FALSE]  # matrix

Leave a Comment