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

Jim Lemon drjimlemon at gmail.com
Sun May 24 05:59:27 CEST 2015


Hi Kathryn,
Well, there's always:

make_num_seq<-function(x=list(c(from=1,to=1,by=1,each=1,times=1))) {
 return(c(unlist(lapply(x,
  function(x) return(rep(seq(x[1],x[2],by=x[3]),each=x[4],times=x[5]))))))
}
make_num_seq(list(c(1,3,1,2,4),c(4,5,1,4,3),c(6,9,1,3,2)))

Jim


On Sun, May 24, 2015 at 7:46 AM, jdnewmil <jdnewmil at dcn.davis.ca.us> wrote:
> 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.
>
>
> ______________________________________________
> 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