luke123
luke123

Reputation: 641

How to modify vector elements based on their position in the sequence

I want another vector where each numeric value is = to it's current value minus (its position in the vector - 1), e.g.

Say I have

positions <- c(9,  30,  46,  52,  76)

I want another vector which is equal to c(9, 29, 44, 49, 72)

thanks

Upvotes: 1

Views: 155

Answers (2)

Paul
Paul

Reputation: 27413

answer <- positions - 0:4

0:4 is the vector c(0,1,2,3,4)

Upvotes: 4

thelatemail
thelatemail

Reputation: 93803

x  <- c(9,  30,  46,  52,  76)
x - (seq_along(x)-1)
[1]  9 29 44 49 72

Upvotes: 6

Related Questions