Reputation: 1294
If I have next vector:
vector <- c(1,6,10)
How can I create the vector 1 2 3 6 7 8 10 11 12
?
Another example:
vector <- c(4,9,15)
My desired vector would be 4 5 6 9 10 11 15 16 17
.
Any help would be great.
Thanks
Upvotes: 0
Views: 27
Reputation: 389235
We can use outer
:
vector <- c(1,6,10)
c(t(outer(vector, 0:2, `+`)))
#[1] 1 2 3 6 7 8 10 11 12
Upvotes: 0
Reputation: 887851
We can do
c( sapply(vector, function(x) x:(x + 2)))
Or with
sort(vector + rep(0:2, each = length(vector)))
Upvotes: 2