How to extract rows from matrices consistently?
Feng, Ken wrote:
Hi, How do I ensure that I always get a matrix back when I extract rows? The mickey-mouse example doesn't matter much, but if instead of 1:2 or 1, I have a vector which may have 1 or more values, then I'm in trouble. Any way to make this consistently return a matrix? Thx in advance. - Ken # ------------------------------------------------------------------------ -------------------------
x <- matrix( 1:10, nrow = 5 )
x
[,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10
class( x[1:2,] )
[1] "matrix" # this is good
class( x[1,] )
[1] "integer" # this is EVIL
class(x[1,,drop=FALSE]) [1] "matrix" # this is good (should be the default, perhaps) vQ