Re: [R] elements in a matrix to a vector

From: Dave Roberts <droberts_at_montana.edu>
Date: Thu 10 Nov 2005 - 04:27:35 EST

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@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@montana.edu
Montana State University
Bozeman, MT 59717-3460

______________________________________________
R-help@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
Received on Thu Nov 10 05:21:01 2005

This archive was generated by hypermail 2.1.8 : Thu 10 Nov 2005 - 09:22:56 EST