[R] ? extended rep()

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Oct 20 23:59:21 CEST 2008


On 20-Oct-08 21:19:21, Stefan Evert wrote:
> On 20 Oct 2008, at 22:57, (Ted Harding) wrote:
>> I'm wondering if there's a compact way to achieve the
>> following. The "dream" is that one could write
>>
>>  rep(c(0,1),times=c(3,4,5,6))
>>
>> which would produce
>>
>> # [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
>>
>> in effect "recycling" x through 'times'.
> 
> rep2 <- function (x, times) rep(rep(x, length.out=length(times)),
> times)
> 
> rep2(c(0,1),times=c(3,4,5,6))
>   [1] 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
> 
> Any prizes for shortest solution? ;-)
> 
> Best,
> Stefan

If ever we are both within reach of 'en øl', then yes.
But Gabor came up with a shorter one.

I tried to shorten Gabor's but failed.

However, all competitors are entitled to a consolation prize!
(And that includes me ... )
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 20-Oct-08                                       Time: 22:59:16
------------------------------ XFMail ------------------------------



More information about the R-help mailing list