[R] Alternating numbers in rep()

Gabor Csardi csardi at rmki.kfki.hu
Thu Jan 24 21:04:13 CET 2008


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



More information about the R-help mailing list