[R] Matrix to "indexed" vector

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Tue Jan 11 13:55:48 CET 2005


maybe something like:

mat <- rnorm(9); dim(mat) <- c(3,3)
mat
###########
cbind(i=rep(1:nrow(mat), each=ncol(mat)),
                j=rep(1:ncol(mat), nrow(mat)), value=c(t(mat)))

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Sean Davis" <sdavis2 at mail.nih.gov>
To: "r-help" <r-help at stat.math.ethz.ch>
Sent: Tuesday, January 11, 2005 1:35 PM
Subject: [R] Matrix to "indexed" vector


>I have a matrix that I want to turn into a transformed matrix that 
>includes the indices from the original matrix and the value.  The 
>matrix is simply real-valued and is square (and large (8k x 8k)).  I 
>want something that looks like (for the 3x3 case):
>
> i j value
> 1 1 1.0
> 1 2 0.783432
> 1 3 -0.123482
> 2 1 0.783432
> 2 2 1.0
> 2 3 0.928374
>
> and so on....
>
> I can do this with for loops, but there is likely to be a better way 
> and for my own edification, I would like to see what others would 
> do.  I am sinking the results to a file for loading into SQL 
> database.
>
> Thanks,
> Sean
>
> ______________________________________________
> 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