Reputation: 23567
I have a vector called z:
x<-matrix(rep(-0.02,12))
y<-matrix(rep(0.03,12))
a<-rbind(x,y)
z<-cumprod(1+a) #
The initial point and the last point in the Z vector are 0.98 and 1.12. The length of the series is 24. The shortest distance between the two is by travelling in increments of:
(1.12 - 0.98) / (24-1) = 0.0060355
In order to get the series I must add 0.98 to 0.0060355 to get element 2. To get element 3, I must take element 2 and add 0.0060355. How can one do this most efficiently in R rather than a loop that requires looping and referencing the previous element? Is this possible?
Upvotes: 0
Views: 3149
Reputation: 263331
Rather than calculating the increment, let seq
do it for you:
seq(z[1], z[length(z)], length=length(z) )
Upvotes: 1
Reputation: 5366
One other solution would be to create a new vector w and to use the cumsum function :
w <- c(.98, rep(0.0060355,23)
cumsum(w)
Upvotes: 3
Reputation: 8126
It seems you want equally spaced intervals in your new sequence. In which case, I think you just want to use the seq
command
seq(z[1],z[length(z)],(z[length(z)]-z[1])/(length(z)-1))
Upvotes: 1