[R] How to extract rows from matrices consistently?

From: Feng, Ken <ken.feng_at_nikkociti.com>
Date: Thu, 12 Jun 2008 17:46:28 +0900


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.

#



> 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 [[alternative HTML version deleted]] ______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 12 Jun 2008 - 10:46:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 Jun 2008 - 11:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive