[R] R annoyances

Thomas Lumley tlumley at u.washington.edu
Thu May 19 16:41:12 CEST 2005


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




More information about the R-help mailing list