[R] ? extended rep()

Stefan Evert stefan.evert at uos.de
Mon Oct 20 23:19:21 CEST 2008


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



More information about the R-help mailing list