Dekike
Dekike

Reputation: 1294

If I have the vector `2, 7, 12`, how can I create the vector `2, 2+1, 2+2, 7, 7+1, 7+2, 12, 12+1, 12+2`?

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

Answers (2)

Ronak Shah
Ronak Shah

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

akrun
akrun

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

Related Questions