[R] elegant way to create a sequence with the 'rep' bulit-in function

Bert Gunter gunter.berton at gene.com
Sat May 23 21:52:01 CEST 2015


Elegance is in the eye of the beholder.

But I would have thought that anything you do would be some variation of:

c(rep(1:3,e=2,time=4),
rep(4:5,e=4,time=3),
rep(6:9,e=3,time=2) )

## yielding

 [1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 5 4
4 4 4 5 5 5 5 4
[42] 4 4 4 5 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 6 6 6 7 7 7 8 8 8 9 9 9

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Sat, May 23, 2015 at 7:55 AM, Kathryn Lord
<kathryn.lord2000 at gmail.com> wrote:
> Dear R users,
>
> I'd like to create a sequence/vector, for example,
>
> 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3       4 4 4 4 5 5 5 5 4 4 4
> 4 5 5 5 5 4 4 4 4 5 5 5 5       6 6 6 7 7 7 8 8 8 9 9 9 6 6 6 7 7 7 8 8 8 9
> 9 9
>
> So I did like this below.
>
> a <- 4
> b <- 3
> c <- 2
>
> grp <- c( rep(1:b, each=c, times=a), rep(1:c, each=a, times=b)+b, rep(1:a,
> each=b, times=c)+b+c )
>
> I wonder if there is a more elegant way to do this?
>
> Any suggestions? Thank you!
>
> Best wishes
>
> Kathie
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list