Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.A41.4.61b.0505190740350.13738@homer09.u.washington.edu>
Date: 2005-05-19T14:41:12Z
From: Thomas Lumley
Subject: R annoyances
In-Reply-To: <AF003EF88447964B88823C3F50A6AB750ACFD46B@gsnbp25es.firmwide.corp.gs.com>

On Thu, 19 May 2005, Chalasani, Prasad wrote:
> (b) When extracting 2 or more columns of a matrix,
>    R returns the result as a matrix, BUT when extracting
>    just one column, it returns a vector/array, rather than
>    a matrix, so I've to keep doing as.matrix, which is annoying.
>
> 	sub.mtx <- as.matrix(mtx[,1])
>
> 	Of course I could write a suitable function
> 		cols <- function(mtx,range) as.matrix(mtx[, range])
> 	but then I lose the syntactic sugar of being able to say "[,1]".

This one is actually a FAQ,
         mtx[,1,drop=FALSE]

 	-thomas