Mike
Mike

Reputation: 1141

How to add a sequence of numbers around each value in a vector

I've got a vector of numbers:

vec <- c(50, 75, 100, 125, 150, 200, 250, 300, 350, 400, 450, 475, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000)

I'd like to add the 10 numbers above and 10 numbers below each value. E.g. for 50 you would add 40 through to 49 and 51 through to 60.

Any help much appreciated!

Upvotes: 0

Views: 88

Answers (2)

akrun
akrun

Reputation: 886938

We can use outer from base R

c(outer(-10:10, vec, `+`))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388797

Using sapply :

c(sapply(vec, `+`, -10:10))

If the number overlap you might want to add unique to the above output to get only unique values.

Upvotes: 2

Related Questions