 # How to Transpose a matrix put in one row

For example, my matrix is
1, 2, 3,
4,5,6
7,8,9
And I want is
1,
2
3
4
5
6
7
8
9.
Thanks

mym <- matrix(1:9,nrow=3,ncol=3)

mym2 <- matrix(NA,nrow=9,ncol=1)

mym2[1:9] <- mym

1 Like

``````m <- matrix(1:9, nrow=3, byrow=TRUE)
m
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    4    5    6
#> [3,]    7    8    9

# Convert to vector by row
m2 = c(t(m))
m2
#>  1 2 3 4 5 6 7 8 9

# Convert to vector by column
m3 = c(m)
m3
#>  1 4 7 2 5 8 3 6 9
``````

Created on 2020-07-05 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

However my data looks like this when I use your method in R
qw<-matrix(March:April:May,nrow = 3,ncol = 51)
qw2<-matrix(March:April:May,nrow = 153,ncol=1)
qw2
qw2[1:153]<-qw
qw2
it return this
[,1]
[1,] 8568065
[2,] 8568066
[3,] 8568067
[4,] 8568068
[5,] 8568069
[6,] 8568070
[7,] 8568071
....
thanks

So... Exactly what you asked for ...