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.
On Apr 6, 2010, at 6:39 AM, Robert M. Flight wrote:> 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, > > -RobertSee R FAQ 7.5: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f HTH, Marc Schwartz
yes, there is -- have a look at the online help file ?"[", especially argument 'drop'. Try also this: crud <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4) crud[, 1, drop = FALSE] I hope it helps. Best, Dimitris On 4/6/2010 1:39 PM, Robert M. Flight wrote:> 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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014