Hi, I would like to be able to repeat a string within a command. I think there is an easy way but I can not figure out how. Here is an example. x<-1:15 I would like to turn this into the following matrix: xm<-cbind(x,x,x,x,x) But I would like to do so by having a command that repeats x within the cbind command. Does that make sense? I have tried various things mainly involving the commands "rep" and "paste" but cannot get it. cbind(rep('x',times=5)) I am guessing that this is not good programming etiquette? Something seems off about it. But still, I would like to know how to do it. I do understand that I can use a for loop to easily accomplish the desired output but am looking for a solution similar to the above syntax. Thank you for any ideas, Xu Wang -- View this message in context: http://n4.nabble.com/expand-the-inside-of-command-with-rep-tp1476420p1476420.html Sent from the R help mailing list archive at Nabble.com.
Something like this:> do.call( cbind, rep(list(1:10), 5) )[,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 2 2 2 2 2 [3,] 3 3 3 3 3 [4,] 4 4 4 4 4 [5,] 5 5 5 5 5 [6,] 6 6 6 6 6 [7,] 7 7 7 7 7 [8,] 8 8 8 8 8 [9,] 9 9 9 9 9 [10,] 10 10 10 10 10 Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Xu Wang > Sent: Wednesday, February 10, 2010 1:28 PM > To: r-help at r-project.org > Subject: [R] expand the inside of command with rep ? > > > Hi, > > I would like to be able to repeat a string within a command. I think > there > is an easy way but I can not figure out how. > > Here is an example. > > x<-1:15 > I would like to turn this into the following matrix: > xm<-cbind(x,x,x,x,x) > > But I would like to do so by having a command that repeats x within the > cbind command. Does that make sense? I have tried various things mainly > involving the commands "rep" and "paste" but cannot get it. > > cbind(rep('x',times=5)) > > I am guessing that this is not good programming etiquette? Something > seems > off about it. But still, I would like to know how to do it. > > I do understand that I can use a for loop to easily accomplish the > desired > output but am looking for a solution similar to the above syntax. > > Thank you for any ideas, > > Xu Wang > -- > View this message in context: http://n4.nabble.com/expand-the-inside- > of-command-with-rep-tp1476420p1476420.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
try this:> x <- 1:5 > x.list <- lapply(1:15, function(a) x) > do.call(cbind, x.list)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [2,] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 [3,] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 [4,] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 [5,] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5>On Wed, Feb 10, 2010 at 3:27 PM, Xu Wang <xuwang762 at gmail.com> wrote:> > Hi, > > I would like to be able to repeat a string within a command. I think there > is an easy way but I can not figure out how. > > Here is an example. > > x<-1:15 > I would like to turn this into the following matrix: > xm<-cbind(x,x,x,x,x) > > But I would like to do so by having a command that repeats x within the > cbind command. Does that make sense? I have tried various things mainly > involving the commands "rep" and "paste" but cannot get it. > > cbind(rep('x',times=5)) > > I am guessing that this is not good programming etiquette? Something seems > off about it. But still, I would like to know how to do it. > > I do understand that I can use a for loop to easily accomplish the desired > output but am looking for a solution similar to the above syntax. > > Thank you for any ideas, > > Xu Wang > -- > View this message in context: http://n4.nabble.com/expand-the-inside-of-command-with-rep-tp1476420p1476420.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
To construct the matrix that would result from your cbind() command, do this:> x <- 1:15 > matrix( rep(x,5) , ncol=5)[,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 2 2 2 2 2 [3,] 3 3 3 3 3 [4,] 4 4 4 4 4 [5,] 5 5 5 5 5 [6,] 6 6 6 6 6 [7,] 7 7 7 7 7 [8,] 8 8 8 8 8 [9,] 9 9 9 9 9 [10,] 10 10 10 10 10 [11,] 11 11 11 11 11 [12,] 12 12 12 12 12 [13,] 13 13 13 13 13 [14,] 14 14 14 14 14 [15,] 15 15 15 15 15 This doesn't, of course, use cbind() at all. It uses a different and simpler method to directly construct the matrix. You can easily generalize it to more columns by using a variable instead of 5 in the rep() command, and for the ncol arg to matrix(). -Don At 12:27 PM -0800 2/10/10, Xu Wang wrote:>Hi, > >I would like to be able to repeat a string within a command. I think there >is an easy way but I can not figure out how. > >Here is an example. > >x<-1:15 >I would like to turn this into the following matrix: >xm<-cbind(x,x,x,x,x) > >But I would like to do so by having a command that repeats x within the >cbind command. Does that make sense? I have tried various things mainly >involving the commands "rep" and "paste" but cannot get it. > >cbind(rep('x',times=5)) > >I am guessing that this is not good programming etiquette? Something seems >off about it. But still, I would like to know how to do it. > >I do understand that I can use a for loop to easily accomplish the desired >output but am looking for a solution similar to the above syntax. > >Thank you for any ideas, > >Xu Wang >-- >View this message in context: >http://*n4.nabble.com/expand-the-inside-of-command-with-rep-tp1476420p1476420.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
Hi Xu, Try replicate(5, 1:15) HTH, Jorge On Wed, Feb 10, 2010 at 3:27 PM, Xu Wang > wrote:> > Hi, > > I would like to be able to repeat a string within a command. I think there > is an easy way but I can not figure out how. > > Here is an example. > > x<-1:15 > I would like to turn this into the following matrix: > xm<-cbind(x,x,x,x,x) > > But I would like to do so by having a command that repeats x within the > cbind command. Does that make sense? I have tried various things mainly > involving the commands "rep" and "paste" but cannot get it. > > cbind(rep('x',times=5)) > > I am guessing that this is not good programming etiquette? Something seems > off about it. But still, I would like to know how to do it. > > I do understand that I can use a for loop to easily accomplish the desired > output but am looking for a solution similar to the above syntax. > > Thank you for any ideas, > > Xu Wang > -- > View this message in context: > http://n4.nabble.com/expand-the-inside-of-command-with-rep-tp1476420p1476420.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]