[R] respecting original matrix dimensions

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Apr 6 13:52:15 CEST 2010


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



More information about the R-help mailing list