[R] matrix with ncol=1

Thomas Lumley tlumley at u.washington.edu
Fri Feb 1 19:15:23 CET 2002


On Fri, 1 Feb 2002, Christian Hennig wrote:

> Hello list,
>
> I try to optimize some R code and it turns out that the function as.matrix
> takes a lot of time in my code. There is only one reason why I need
> as.matrix: My code should work for matrices with ncol=p and p should be
> allowed to be 1 or larger.
>
> Now I have a matrix x with dim(x)=(n,p), and
> I need to work with the submatrix y <-x[gv,], (gv a vector of
> n logicals) and to calculate cov(y) and other functions for matrices.
> Unfortunately, if p=1, y is no longer a matrix, cov(y) gives an error,
> and therefore I use y <- as.matrix(x[gv,]) which makes my program
> surprisingly slower (this is because lots of gv-vectors are needed).
>
> Is there any way to handle submatrices of matrices with p=1 in the same
> manner than with p>1 without using as.matrix?

Yes. It's a FAQ. x[gv,,drop=FALSE]


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list