Home > Software design >  How to transpose a matrix with fixed row spacing using R language?
How to transpose a matrix with fixed row spacing using R language?

Time:06-22

How to transpose the following matrix every six rows using R language? That is, after transposition, the first row becomes numbers 1-6, the second row becomes numbers 7-12, and so on. Finally, a 10*6 matrix is obtained.

simple <- matrix(1:60,nrow=60,ncol=1)

Sincerely ask all scholars.

CodePudding user response:

You could enable byrow

> matrix(simple, ncol = 6, byrow = TRUE)
      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    1    2    3    4    5    6
 [2,]    7    8    9   10   11   12
 [3,]   13   14   15   16   17   18
 [4,]   19   20   21   22   23   24
 [5,]   25   26   27   28   29   30
 [6,]   31   32   33   34   35   36
 [7,]   37   38   39   40   41   42
 [8,]   43   44   45   46   47   48
 [9,]   49   50   51   52   53   54
[10,]   55   56   57   58   59   60

CodePudding user response:

Or with dim

t(`dim<-`(simple, c(6, 10)))
       [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    1    2    3    4    5    6
 [2,]    7    8    9   10   11   12
 [3,]   13   14   15   16   17   18
 [4,]   19   20   21   22   23   24
 [5,]   25   26   27   28   29   30
 [6,]   31   32   33   34   35   36
 [7,]   37   38   39   40   41   42
 [8,]   43   44   45   46   47   48
 [9,]   49   50   51   52   53   54
[10,]   55   56   57   58   59   60
  • Related