Re: [R] Vector extracted from a matrix. How can I specify dimensions in as.matrix?

From: Cyril Goutte <>
Date: Fri 28 Jul 2006 - 01:58:14 EST

I think the confusion relates to the fact that in R vectors are not 2D arrays with one dimension set to 1.

So your 'simplevector' is not a vector, and your 'extractedvector3x1' and 'extractedvector1x3' are in fact not 3x1 or 1x3, they are 3-element vectors, which sometimes behave like 1x3 arrays, sometimes not.

I also found that very confusing in the beginning. You have to either keep that in mind in your code or use the 'drop' option if that is convenient.


On Thu, 2006-27-07 at 09:23 -0400, Neuro LeSuperHéros wrote:
> Transpose vector extracted from a matrix
> Hello,
> I am doing a recursive analysis that uses every line (vector) of a matrix in
> a loop. In the model, I need to transpose those vectors that are extracted
> from a matrix.
> Using simple vectors (no matrix involved) the transpose function works fine:
> simplevector <-matrix(1:3,3,1)
> tsimplevector <-t(simplevector) #transposed
> dim(simplevector) #3x1 matrix (vector)
> dim(tsimplevector)#1x3
> PROBLEM: However, when the vector is extracted from a matrix, its dimension
> is NULL. In this case the transposed dimension is correct, but you'll see
> the next example, that if a row was extracted, the dimension would be wrong:
> initialmatrix <- matrix(1:9,3,3)
> extractedvector <-initialmatrix[,1] # extract first columm as vector
> textractedvector <-t(extractedvector)#transposed
> dim(extractedvector) #NULL!!!! <- not working
> dim(textractedvector)#1x3 as expected
> I have tried to transform the extracted vector as.vector and as.matrix.
> as.vector does not give the what I want (still NULL) and as.matrix can't
> specify the number of row and columns so both vectors are vertical.
> In this example, I extract a column and a row. Notice that both
> as.matrix(vector) have the same dimension. Notice that both transposed
> vectors have 1x3 dimensions.
> initialmatrix <- matrix(1:9,3,3)
> extractedvector3x1 <-initialmatrix[,1] # extract first columm as vector
> extractedvector1x3 <-initialmatrix[1,] # extract first row as vector
> #Both are NULL
> dim(extractedvector3x1) #NULL!!!!
> dim(extractedvector1x3) #NULL!!!!
> #transposed dimensions both 1x3
> dim(t(extractedvector3x1)) #1x3
> dim(t(extractedvector1x3)) #1x3 <- not 3x1
> #as.vector: still NULL
> dim(as.vector(extractedvector3x1)) #NULL!!!!
> dim(as.vector(extractedvector1x3)) #NULL!!!!
> #as.matrix: Both dim at 3x1
> dim(as.matrix(extractedvector3x1)) #3x1
> dim(as.matrix(extractedvector1x3)) #3x1 <- Problem: not 1x3
> #as.matrix transposed: Both dim at 1x3
> dim(as.matrix(t(extractedvector3x1))) #1x3
> dim(as.matrix(t(extractedvector1x3))) #1x3 <- Problem: not 3x1
> How can I get correct dimensions? Is there a way to specify dimensions in
> as.matrix?
> Neuro
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri Jul 28 02:04:33 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 28 Jul 2006 - 04:18:29 EST.

Mailing list information is available at Please read the posting guide before posting to the list.