[R] elegant way to create a sequence with the 'rep' bulit-in function
jdnewmil
jdnewmil at dcn.davis.ca.us
Sat May 23 23:46:30 CEST 2015
I agree with Bert. It is not clear what is generating a need for this
sequence, so it
is difficult to see what aspects need to be adjustable. If this specific
sequence is the only one you need, then Bert's code looks "elegant" to
me.
One note: "c" is a base function in R. Functions in R are first-class
objects.
It is really confusing and just a bad idea to create a vector named "c"
and then
call "c" as well. I have used uppercase variable names to alleviate this
problem.
A <- 4
B <- 3
C <- 2
# Minutely more efficient version of OP code:
c( rep( seq.int( B ) , each=C, times=A )
, rep( seq.int( C ) + B , each=A, times=B )
, rep( seq.int( A ) + B + C, each=B, times=C )
)
# Excessively flexible version of OP code (can make V longer):
V <- c( A, B, C )
M <- cbind( embed( c( V[ -1 ], V ), length( V ) ), cumsum( V ) - V[ 1 ]
)
do.call( c
, lapply( seq.int( nrow( M ) )
, function( i ) { rep( seq.int( M[ i, 3 ] ) + M[ i, 4 ],
each=M[ i, 2 ], times=M[ i, 1 ] ) }
)
)
Please post using plain text format to insure that we see what you see,
since the mailing list WILL remove HTML.
On 2015-05-23 12:52, Bert Gunter wrote:
> 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.
>
> ______________________________________________
> 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