[R] elements in a matrix to a vector
Mike Jones
MikeJones at westat.com
Wed Nov 9 18:44:23 CET 2005
my apologies to everyone taking the time to answer this question. i
didn't explain myself very well.
i have the indices of the elements of the matrix and the matrix won't
necessarily have zeros everywhere else. is there a way to use the
indices to strip the elements i need?
thanks again...mj
-----Original Message-----
From: Dave Roberts [mailto:droberts at montana.edu]
Sent: Wednesday, November 09, 2005 12:28 PM
To: Mike Jones
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] elements in a matrix to a vector
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