Hey guys, I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3... with the '1, 2, 3' repeated over 10 times. Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3? Anyways, any help with be greatly appreciated! -- View this message in context: http://www.nabble.com/Sequence-generation-tp25205593p25205593.html Sent from the R help mailing list archive at Nabble.com.
I think should work rep(c(1,2,3),10) Alfredo On Sat Aug 29 15:14:15 EDT 2009, njhuang86 <njhuang86 at yahoo.com> wrote:> > Hey guys, > > I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, > 2, 3, 1, > 2, 3... with the '1, 2, 3' repeated over 10 times. > > Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, > 3, 3? > > Anyways, any help with be greatly appreciated! > -- View this message in context: > http://www.nabble.com/Sequence-generation-tp25205593p25205593.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. > >-- RIOS,ALFREDO ARTURO
On Aug 29, 2009, at 3:14 PM, njhuang86 wrote:> > Hey guys, > > I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2, > 3, 1, > 2, 3... with the '1, 2, 3' repeated over 10 times.As noted earlier rep(1:3, 10)> > Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3??gl the gl function will return a factor and it can be converted to a vector: > gl(10, 3) [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 Levels: 1 2 3 4 5 6 7 8 9 10 > as.numeric(gl(10,3)) [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 Or you can cobble something together like: > floor(1 + 0:29 / 3) [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 which depends on the higher precedence of the ":" operator over infix-"+" and "/". ?Syntax -- David Winsemius, MD Heritage Laboratories West Hartford, CT
On Sat, Aug 29, 2009 at 8:14 PM, njhuang86<njhuang86 at yahoo.com> wrote:> > Hey guys, > > I was wondering how to create this sequence: 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, > 2, 3... with the '1, 2, 3' repeated over 10 times.rep(1:3,10) # rep repeats its first argument according to the number in its second argument> Also, is there a simple method to generate 1, 1, 1, 2, 2, 2, 3, 3, 3?The second argument can be a vector, so what you want here is: rep(1:3,c(3,3,3)) but you can create the second vector here also using rep! Hence: rep(1:3,rep(3,3)) Barry