Reputation: 33
Consider some vector in R: x
x<-1:10
I'd like to create a repeating sequence of x
, with the first element of each sequence truncated with each repetition, yielding the same output as would be given by issuing the following command in R:
c(1:10,2:10,3:10,4:10,5:10,6:10,7:10,8:10,9:10,10:10)
Can this be done? In reality, I'm working with a much larger vector for x
. I'm playing with numerous combinations of the rep()
function, to no avail.
Upvotes: 3
Views: 204
Reputation: 193517
Here's an alternative using mapply
:
unlist(mapply(":", 1:10, 10))
# [1] 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 4 5 6 7
# [25] 8 9 10 4 5 6 7 8 9 10 5 6 7 8 9 10 6 7 8 9 10 7 8 9
# [49] 10 8 9 10 9 10 10
Upvotes: 4
Reputation: 3224
A bit of a hack, because you can decompose what you are trying to do into two sequences:
rep(0:9, 10:1) + sequence(10:1)
You can see what each part does. I don't know if there is a way to feed the parameters to rep()
or seq()
like you would do in a Python expansion.
Upvotes: 2