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
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
>
On Jan 11, 2005, at 7:55 AM, Dimitris Rizopoulos wrote:> 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))) >That will do it. Thanks! Sean
Sean Davis wrote:> 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 >How about: x <- c(1.0, 0.783432, -0.123482, 0.783432, 1.0, 0.928374) x <- matrix(x, 2, 3, TRUE) y <- cbind(expand.grid(i = seq(nrow(x)), j = seq(ncol(x))), c(x)) y[order(y[, 1], y[, 2]), ] i j c(x) 1 1 1 1.000000 3 1 2 0.783432 5 1 3 -0.123482 2 2 1 0.783432 4 2 2 1.000000 6 2 3 0.928374 HTH, --sundar
Hi Sean,
Here is a way to do that:
idmatrix <- function(m){
if (!is(m,"matrix")) stop("Please provide a matrix as
argument")
ind <- cbind(as.vector(row(m)),as.vector(col(m)))
out <- cbind(ind, m[ind])
return(out)
}
There are others way to provide indexes, such as
> expand.grid(1:nrow(mm),1:ncol(mm))
Not sure which is the most efficient.
HTH,
Eric
> data(iris)
> mm=as.matrix(iris[1:5,1:4])
> idmatrix(mm)
[,1] [,2] [,3]
[1,] 1 1 5.1
[2,] 2 1 4.9
[3,] 3 1 4.7
[4,] 4 1 4.6
[5,] 5 1 5.0
[6,] 1 2 3.5
[7,] 2 2 3.0
[8,] 3 2 3.2
[9,] 4 2 3.1
[10,] 5 2 3.6
[11,] 1 3 1.4
[12,] 2 3 1.4
[13,] 3 3 1.3
[14,] 4 3 1.5
[15,] 5 3 1.4
[16,] 1 4 0.2
[17,] 2 4 0.2
[18,] 3 4 0.2
[19,] 4 4 0.2
[20,] 5 4 0.2
At 13:35 11/01/2005, Sean Davis wrote:>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
Eric Lecoutre
UCL / Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium
tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
If the statistics are boring, then you've got the wrong numbers. -Edward
Tufte