[R] elements in a matrix to a vector

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Nov 9 18:13:00 CET 2005


"Mike Jones" <MikeJones at westat.com> writes:

> 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

You need to explain the rules of the game better. Do you mean like


> M <- matrix(c(0,0,3,2,0,0,0,4,0),3,byrow=T)
> rowSums(M)
[1] 3 2 4

or

> x <- c(t(M)) ; x[x!=0]
[1] 3 2 4

or

...?

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list