tags:

views:

43

answers:

2

(please ignore this question - it is foolish...)

I want something that will do this:

rep(1:3, each = 1:3)
# And will output this vector:
c(1,2,2,3,3,3)

Does it exist? (and if so, how?)

Update: I can write it like this -

rep2 <- function(x, each)
{
    output <- NULL
    for(i in 1:length(x))
    {
        output <- c(output, rep(x[i], each = each[i]))
    }
    return(output)
}
# example:
rep2(1:3,1:3)

But am hoping for a smarter solution...

+1  A: 

Ummm this:

> rep(1:3, 1:3)
[1] 1 2 2 3 3 3

You want the 'times=' argument, not 'each='.

Spacedman
<blushing> - thanks...
Tal Galili
+2  A: 
rep(1:3,1:3)

Surely that works as your example. For info, the argument for the rep function is times, each is a single integer (or the first element of a vector) which determines the repetition of all elements of x.

James
<blushing> - thanks...
Tal Galili