[R] elements in a matrix to a vector

roger koenker rkoenker at uiuc.edu
Wed Nov 9 18:14:33 CET 2005


If you are really looking for a way to extract the non-zero elements you
can use something like the following:

 > library(SparseM)
 >  A
      [,1] [,2] [,3]
[1,]    0    0    3
[2,]    2    0    0
[3,]    0    4    0
 > as.matrix.csr(A)@ra
[1] 3 2 4

there is a tolerance parameter in the coercion to sparse representation
to decide what is really "zero"  -- by default this is  eps = .Machine 
$double.eps.


url:    www.econ.uiuc.edu/~roger            Roger Koenker
email    rkoenker at uiuc.edu            Department of Economics
vox:     217-333-4558                University of Illinois
fax:       217-244-6678                Champaign, IL 61820


On Nov 9, 2005, at 10:14 AM, 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




More information about the R-help mailing list