[R] respecting original matrix dimensions

Robert M. Flight rflight79 at gmail.com
Tue Apr 6 13:39:45 CEST 2010


Hi All,

I'm hoping someone else can help me out with this. I am doing some
matrix algebra using sub-parts of matrices, and sometimes I need only
a single row/column of the original matrix. However, whenever I pull
out only a single row/column, R returns a row vector, but often this
will break my matrix algebra. Is there any *easy* way to get R to
remember what the single row/column came out of and format the return
appropriately?

Example:

crud <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4)
tmp <- crud[,1]
#tmp gives:  [1] 1 2 3

what I actually want is:
tmp <- crud[,1]
#tmp gives
     [,1]
[1,]    1
[2,]    2
[3,]    3

I don't see an easy way to do this beyond using matrix and supplying
the correct matrix dimensions based on the indexing, or using a
conditional to check if only one column/row is being taken out.

Any help would be very much appreciated.

Regards,

-Robert


Robert M. Flight, Ph.D.
Bioinformatics and Biomedical Computing Laboratory
University of Louisville
Louisville, KY

PH 502-852-0467
EM robert.flight at louisville.edu
EM rflight79 at gmail.com

Williams and Holland's Law:
       If enough data is collected, anything may be proven by
statistical methods.



More information about the R-help mailing list