[R] transposing a matrix - row by row?
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Thu Apr 23 16:45:06 CEST 2009
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
More information about the R-help
mailing list