I hve been trying to add a lag into an R loop but haven't been having much luck. The bare basics of my data looks like this:
N1<-1
R1<-1
N1<-(R1-3)
R2<-R1*2 #note this is different from other Rx<-...
N2<-(R2-3)
R3<-(R2-R1)*2
N3<-(R3-3)
R4<-(R3-R2)*2
N4<-(R4-3)
R5<-(R4-R3)*2
N4<-(R4-3)
R5<-(R4-R3)*2
N4<-(R4-3)
R5<-(R4-R3)*2
etc.. to R10
print(R10)
I can turn it into a loop like so to simplify it:
N1<-1
R1<-1
N1<-(R1-3)
R2<-R1*2
n<-N1
r<-R2
for(i in 1:8) n<-(r-3)
r<-(r-("r-1"))*2
print(r)
However the "r-1" phrase (conceptually meaning the previous iteration of r, not literally meaning r minus the integer one) does not work as r is overwritten with each iteration of the loop. An ideas on how to do this? Lag functions I've read up on so far seem to require a dataframe which, as this is a model, I don't have. I'm also unsure if there's just some terminology for "r-1", or even lag, which I'm missing. Any help would be much appreciated