Hi, I am trying to transform a data matrix into a vector and have not be able to accomplish want I am looking for. The setup is as follows. I start with a 3 x 3 matrix: 5 1 3 3 3 2 1 2 4 I would like to transorm it into a 27 x 1 vector of the follwing form 5 5 5 1 1 1 3 3 3 . . . 1 1 1 2 2 2 4 4 4 In essense I want to create a vector in which each element of the original matrix is repeated three times, starting with cell [1,1] and ending with cell [3,3]. This is small example of the larger problem I am trying to solve. The actual data matrix is 352 x 15, and I would like to end up with a 26,400 x 1 vector. So each element of the data matrix is repeated 5 times. Any suggestions or hints would be much appreciated. Thanks! I am using; R version 2.12.2 (2011-02-25) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-pc-mingw32/i386 (32-bit) Cheers, Charles
Transforming a data matrix into a vector
4 messages · Charles Ellis, Richard M. Heiberger, Jorge Ivan Velez
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110530/14251df1/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110530/3fe997b8/attachment.pl>
Brilliant! Works like a charm. Thanks both. Cheers, Charles From: Richard M. Heiberger [mailto:rmh at temple.edu] Sent: Monday, May 30, 2011 4:36 PM To: Charles Ellis Cc: r-help at R-project.org Subject: Re: [R] Transforming a data matrix into a vector
tmp <- matrix(c(5,3,1, 1,3,2, 1,2,4), 3, 3) tmp
???? [,1] [,2] [,3] [1,]??? 5??? 1??? 1 [2,]??? 3??? 3??? 2 [3,]??? 1??? 2??? 4
rep(t(tmp), each=3)
?[1] 5 5 5 1 1 1 1 1 1 3 3 3 3 3 3 2 2 2 1 1 1 2 2 2 4 4 4
On Mon, May 30, 2011 at 4:22 PM, Charles Ellis <CharlesE at mairesearch.com> wrote:
Hi, I am trying to transform a data matrix into a vector and have not be able to accomplish want I am looking for. ?The setup is as follows. I start with a 3 x 3 matrix: 5 1 3 3 3 2 1 2 4 I would like to transorm it into a 27 x 1 vector of the follwing form 5 5 5 1 1 1 3 3 3 . . . 1 1 1 2 2 2 4 4 4 In essense I want to create a vector in which each element of the original matrix is repeated three times, starting with cell [1,1] and ending with cell [3,3]. This is small example of the larger problem I am trying to solve. ?The actual data matrix is 352 x 15, and I would like to end up with a 26,400 x 1 vector. ?So each element of the data matrix is repeated 5 times. Any suggestions or hints would be much appreciated. ?Thanks! I am using; R version 2.12.2 (2011-02-25) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-pc-mingw32/i386 (32-bit) Cheers, Charles ______________________________________________ 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.