# Re: [R] Alternating numbers in rep()

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Thu, 24 Jan 2008 13:36:34 -0700

The last one can be shortened to:

> c(aperm(array(1:4, 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

And it extends to 38 fairly easily:

> c(aperm(array(1:38, c(2,38/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 5
[26] 6 5 6 5 6 5 6 5 6 5 6 7 8 7 8 7 8 7 8 7 8 7 8 9 10
[51] 9 10 9 10 . . .

```--
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 Greg Snow
> Sent: Thursday, January 24, 2008 1:33 PM
> To: David Afshartous; r-help_at_r-project.org
> Subject: Re: [R] Alternating numbers in rep()
>
> 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
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help