Re: [R] How to extract rows from matrices consistently?

From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
Date: Thu, 12 Jun 2008 11:24:35 +0200

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



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 - 09:28:23 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 - 09: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