All, I'm trying to obtain a one-liner to generate a certain sequence of alternatign numbers. Consider:> unlist(rep(list(c(1,2), c(3,4)), each = 6))[1] 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4 I'd like the result to be as above but continue until 38. Of course, I could hardcode this going up to c(37,38), but is there a more elegant way? Thanks! David
On Thu, Jan 24, 2008 at 03:03:22PM -0500, David Afshartous wrote:> > All, > > I'm trying to obtain a one-liner to generate a certain sequence of > alternatign numbers. > > Consider: > > unlist(rep(list(c(1,2), c(3,4)), each = 6)) > [1] 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4 > > I'd like the result to be as above but continue until 38. Of course, I > could hardcode this going up to c(37,38), but is there a more elegant way?It is a matter of taste as well, but i think as.vector(apply(matrix(1:38, nr=2), 2, rep, 6)) is not bad. Gabor> > Thanks! > David > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
This ?? as.vector( outer(0:1, rep(seq(1,37,by=2), each=6), "+" ) ) On Thu, 24 Jan 2008, David Afshartous wrote:> > All, > > I'm trying to obtain a one-liner to generate a certain sequence of > alternatign numbers. > > Consider: >> unlist(rep(list(c(1,2), c(3,4)), each = 6)) > [1] 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4 > > I'd like the result to be as above but continue until 38. Of course, I > could hardcode this going up to c(37,38), but is there a more elegant way? > > > Thanks! > David > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Here are a couple of options (I'm sure there are plenty of others) that replicate your results and can be extended without too much more typing (but some arithmatic):> unname(unlist( rep( as.data.frame( matrix(1:4, nrow=2) ) , each=6 ) ))[1] 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4> c(aperm(array(rep( 1:4, 6), c(2,2,6)),c(1,3,2)))[1] 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4 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 David Afshartous > Sent: Thursday, January 24, 2008 1:03 PM > To: r-help at r-project.org > Subject: [R] Alternating numbers in rep() > > > All, > > I'm trying to obtain a one-liner to generate a certain > sequence of alternatign numbers. > > Consider: > > unlist(rep(list(c(1,2), c(3,4)), each = 6)) > [1] 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4 > > I'd like the result to be as above but continue until 38. Of > course, I could hardcode this going up to c(37,38), but is > there a more elegant way? > > > Thanks! > David > > ______________________________________________ > 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. >