(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…
Surely that works as your example. For info, the argument for the
repfunction istimes,eachis a single integer (or the first element of a vector) which determines the repetition of all elements ofx.