Dear All I would like to convert matrix rows to columns. I am thinking the t() function should help, but am having a hard time converting the matrix into the dimensions I would like them to. Example: a <-matrix(c(1:30),ncol=3) gives me:[,1] [,2] [,3] [1,] 1 11 21 [2,] 2 12 22 [3,] 3 13 23 [4,] 4 14 24 [5,] 5 15 25 [6,] 6 16 26 [7,] 7 17 27 [8,] 8 18 28 [9,] 9 19 29 [10,] 10 20 30 I would like to convert the above into a matrix that looks like this with 3 rows and 10 columns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 I have tried : b <-t(matrix(a)) but I am getting a 1X30 double matrix. I also Tried to specify b <-t(matrix(a),byrow=TRUE,ncol=10) but that does not help either... apreciate your help, Andras [[alternative HTML version deleted]]
try this: a <- matrix(1:30, ncol= 3 ) t(a) I hope it helps. Best, Dimitris On 11/7/2012 12:50 PM, Andras Farkas wrote:> Dear All > > I would like to convert matrix rows to columns. I am thinking the t() function should help, but am having a hard time converting the matrix into the dimensions I would like them to. Example: > > a <-matrix(c(1:30),ncol=3) gives me:[,1] [,2] [,3] > [1,] 1 11 21 > [2,] 2 12 22 > [3,] 3 13 23 > [4,] 4 14 24 > [5,] 5 15 25 > [6,] 6 16 26 > [7,] 7 17 27 > [8,] 8 18 28 > [9,] 9 19 29 > [10,] 10 20 30 > > I would like to convert the above into a matrix that looks like this with 3 rows and 10 columns: > > 1 2 3 4 5 6 7 8 9 10 > 11 12 13 14 15 16 17 18 19 20 > 21 22 23 24 25 26 27 28 29 30 > > I have tried : > b <-t(matrix(a)) > > but I am getting a 1X30 double matrix. I also Tried to specify > b <-t(matrix(a),byrow=TRUE,ncol=10) > but that does not help either... > > > > > apreciate your help, > > > > Andras > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- 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 Web: http://www.erasmusmc.nl/biostatistiek/
Hi, ?is.matrix(a) #[1] TRUE #You don't need t(matrix(a)) t(a) #???? [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #[1,]??? 1??? 2??? 3??? 4??? 5??? 6??? 7??? 8??? 9??? 10 #[2,]?? 11?? 12?? 13?? 14?? 15?? 16?? 17?? 18?? 19??? 20 #[3,]?? 21?? 22?? 23?? 24?? 25?? 26?? 27?? 28?? 29??? 30 is.matrix(t(a)) #[1] TRUE str(t(a)) # int [1:3, 1:10] 1 11 21 2 12 22 3 13 23 4 ... str(t(matrix(a)) ) # int [1, 1:30] 1 2 3 4 5 6 7 8 9 10 ... A.K. ----- Original Message ----- From: Andras Farkas <motyocska at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Wednesday, November 7, 2012 6:50 AM Subject: [R] simple coversion of matrix rows to matrix columns Dear All ? I would like to convert matrix rows to columns. I am thinking the t() function should help, but am having a hard time converting the matrix into the dimensions I would like them to. Example: ? a <-matrix(c(1:30),ncol=3) gives me:[,1] [,2] [,3] [1,]? ? 1? 11? 21 [2,]? ? 2? 12? 22 [3,]? ? 3? 13? 23 [4,]? ? 4? 14? 24 [5,]? ? 5? 15? 25 [6,]? ? 6? 16? 26 [7,]? ? 7? 17? 27 [8,]? ? 8? 18? 28 [9,]? ? 9? 19? 29 [10,]? 10? 20? 30 ? I would like to convert the above into a matrix that looks like this with 3 rows and 10 columns: ? 1? ? 2? 3? 4? 5? 6? 7? 8? 9? 10 11? 12? 13? 14? 15? 16? 17? 18? 19? 20 21? 22? 23? 24? 25? 26? 27? 28? 29? 30 ? I have tried : b <-t(matrix(a)) ? but I am getting a 1X30 double matrix. I also Tried to specify b <-t(matrix(a),byrow=TRUE,ncol=10) but that does not help either... ? ? ? ? apreciate your help, ? ? ? Andras ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.