[R] elements in a matrix to a vector

Berton Gunter gunter.berton at gene.com
Wed Nov 9 18:17:01 CET 2005


A matrix is a vector with a dim() attribute. Values are stored in column
major order (first column on top of second column on top of ...). Thus, if
mymatrix is your matrix,

mymatrix[as.logical(mymatrix)] 

gives c(2,4,3), because as.logical(0) = FALSE, as.logical(nonzero) = TRUE.

If you are really doing serious work with sparse matrices , you should look
at the sparseM package.

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
 
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Mike Jones
> Sent: Wednesday, November 09, 2005 8:14 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] elements in a matrix to a vector
> 
> hi all,
> 
> i'm trying to get elements in a matrix into a vector.  i need a
> "streamlined" way to do it as the way i'm doing it is not very
> serviceable.  an example is a 3x3 matrix like
> 
> 0 0 3
> 2 0 0
> 0 4 0
> 
> to a vector like
> 
> 3 2 4
> 
> thanks...mj
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list