One way is to use matrix indexing
matrix(t(m), nrow=nrow(m))[, c(matrix(1:ncol(m), nrow(m), byrow=T)) ]
This takes the transposed matrix and moves the columns in the desired order.
m <- matrix(1:18,nrow=3,ncol=6) matrix(t(m), nrow=nrow(m)) # [,1] [,2] [,3] [,4] [,5] [,6] # [1,] 1 10 2 11 3 12 # [2,] 4 13 5 14 6 15 # [3,] 7 16 8 17 9 18
So, we need the 1st, 3rd and 5th columns and the 2nd, 4th and 6th columns. One way is to index them using
c(matrix(1:ncol(m), nrow(m), byrow=T))
Alternatively you can use
idx <- rep(1:ncol(m), each=nrow(m), length=ncol(m)) ; do.call(cbind, split.data.frame(t(m), idx))
Try a new matrix
(m <- matrix(1:50, nrow=5)) # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # [1,] 1 6 11 16 21 26 31 36 41 46 # [2,] 2 7 12 17 22 27 32 37 42 47 # [3,] 3 8 13 18 23 28 33 38 43 48 # [4,] 4 9 14 19 24 29 34 39 44 49 # [5,] 5 10 15 20 25 30 35 40 45 50 matrix(t(m), nrow=nrow(m))[, c(matrix(1:ncol(m), nrow(m), byrow=T)) ] # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # [1,] 1 2 3 4 5 26 27 28 29 30 # [2,] 6 7 8 9 10 31 32 33 34 35 # [3,] 11 12 13 14 15 36 37 38 39 40 # [4,] 16 17 18 19 20 41 42 43 44 45 # [5,] 21 22 23 24 25 46 47 48 49 50