Apparently some matrices when indexed in a certain way stay matrices, others, indexed in the same way become arrays. Example:
> test = matrix(1:10, nrow = 2)
> class(test)
[1] "matrix"
> class(test[,1:2])
[1] "matrix"
> test = matrix(1:10, nrow = 1)
> class(test)
[1] "matrix"
> class(test[,1:2])
[1] "integer"
I would like to know:
- Why is this a feature and not a bug?
- Is there a polished way to make it stay a matrix? (I mean, sure, I could store
nrow(test)
somewhere and then usematrix(test, nrow = somewhere)
) but i hope that there's a neat one-liner for something that in principle should have worked authomatically.
P.S. Why I find this an issue? Because if I then transpose the vector with t()
(the vector that lost the matrix structure) it gives me a row vector, I wanted a column vector, as it should have been in theory. Also, rowMeans()
doesn't work anymore with that vector.