Skip to content
Back to formatted view

Raw Message

Message-ID: <49F07EF2.5070101@erasmusmc.nl>
Date: 2009-04-23T14:45:06Z
From: Dimitris Rizopoulos
Subject: transposing a matrix - row by row?
In-Reply-To: <dae9a2a60904230731i38775262y7a133e457cf4778d@mail.gmail.com>

one way is the following:

X <- matrix(c(10,20,30,40,50,60), 2, 3,
     dimnames = list(c("1","2"), c("1","2","3")))

tX <- t(X)
cbind(
     rows = c(col(tX)),
     columns = c(row(tX)),
     entries = c(tX)
)


I hope it helps.

Best,
Dimitris


Dimitri Liakhovitski wrote:
> Hello,
> I have a matrix that is a product of tapply on a larger data set.
> Let's assume it looks like this:
> 
> X<-matrix(c(10,20,30,40,50,60),2,3)
> dimnames(X)<-list(c("1","2"),c("1","2","3"))
> (X)
> 
>    1   2   3
> 1 10 30 50
> 2 20 40 60
> 
> Is there an efficient way of transforming this matrix into the following matrix:
> 
> rows columns entries
> 1          1          10
> 1          2          30
> 1          3          50
> 2          1          20
> 2          2          40
> 2          3          60
> 
> 
> Thank you very much!

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014