[R] elements in a matrix to a vector

Dave Roberts droberts at montana.edu
Wed Nov 9 18:27:35 CET 2005


Mike,

    It's not clear whaty way you are doing it now, but this works

 > x <- matrix(c(0,2,0,0,0,4,3,0,0),nrow=3)
 > x
      [,1] [,2] [,3]
[1,]    0    0    3
[2,]    2    0    0
[3,]    0    4    0
 > y <- as.vector(t(x))
 > z <- y[y!=0]
 > z
[1] 3 2 4


Dave



Mike Jones wrote:
> 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
> 
> 


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts                                     office 406-994-4548
Professor and Head                                      FAX 406-994-3190
Department of Ecology                         email droberts at montana.edu
Montana State University
Bozeman, MT 59717-3460




More information about the R-help mailing list