Skip to content
Prev 170571 / 398502 Next

matrix transpose

Here's one way that seems to work:

a <- matrix(1:30,5,6)
# Create a target for the transpose
b <- matrix(1:(nrow(a)*ncol(a)),ncol(a),nrow(a))
# populate b with the transpose of a
for (i in 1:ncol(a)) { b[i,1:(nrow(a))] <- a[1:(nrow(a)),i]}
# Check:  Did it work?
all.equal(b,t(a))

I'm sure there's a more "R-like" way of doing this...

Cheers,
Dan Viar
Chesapeake, VA
On Sun, Feb 15, 2009 at 3:05 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote: