Peng Yu
2009-Sep-17 14:02 UTC
[R] How to generate a matrix where each row (or column) is the same vector?
Hi, I can use the following code to generate a matrix, each column of which is 'x'. But I have to specify '5' twice in the second command. I am wondering if there is a better way to do it.> x=1:10 > matrix(rep(x,5),nc=5) > t(matrix(rep(x,5),nc=5))Regards, Peng
Romain Francois
2009-Sep-17 14:07 UTC
[R] How to generate a matrix where each row (or column) is the same vector?
On 09/17/2009 04:02 PM, Peng Yu wrote:> > Hi, > > I can use the following code to generate a matrix, each column of > which is 'x'. But I have to specify '5' twice in the second command. I > am wondering if there is a better way to do it. > >> x=1:10 >> matrix(rep(x,5),nc=5) >> t(matrix(rep(x,5),nc=5)) > > Regards, > PengThis works for me: > do.call( cbind, rep( list( x ), 5 ) ) > do.call( rbind, rep( list( x ), 5 ) ) Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/yw8E : New R package : sos |- http://tr.im/y8y0 : search the graph gallery from R `- http://tr.im/y8wY : new R package : ant
Romain Francois
2009-Sep-17 14:14 UTC
[R] How to generate a matrix where each row (or column) is the same vector?
On 09/17/2009 04:02 PM, Peng Yu wrote:> > Hi, > > I can use the following code to generate a matrix, each column of > which is 'x'. But I have to specify '5' twice in the second command. I > am wondering if there is a better way to do it. > >> x=1:10 >> matrix(rep(x,5),nc=5) >> t(matrix(rep(x,5),nc=5)) > > Regards, > PengOr this ; > matrix(rep(x,5),nr=length(x)) Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/yw8E : New R package : sos |- http://tr.im/y8y0 : search the graph gallery from R `- http://tr.im/y8wY : new R package : ant
Henrique Dallazuanna
2009-Sep-17 14:16 UTC
[R] How to generate a matrix where each row (or column) is the same vector?
Or: replicate(5, 1:10) On Thu, Sep 17, 2009 at 11:02 AM, Peng Yu <pengyu.ut at gmail.com> wrote:> Hi, > > I can use the following code to generate a matrix, each column of > which is 'x'. But I have to specify '5' twice in the second command. I > am wondering if there is a better way to do it. > >> x=1:10 >> matrix(rep(x,5),nc=5) >> t(matrix(rep(x,5),nc=5)) > > Regards, > Peng > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O