problem about link relative ratio

#1
hi
I need to compute link relative ratio, i.e, (this year - last year )/ last year . see my example:
Code:
> myData<-data.frame(year=c(2000,2001,2002,2003,2004),value=c(99,100,110,220,330))
> myData
  year value
1 2000    99
2 2001   100
3 2002   110
4 2003   220
5 2004   330
I need result
Code:
  year value    lrr
1 2000    99 0.0000
2 2001   100 0.0101
3 2002   110 0.1000
4 2003   220 1.0000
5 2004   330 0.5000
And I know a for statement can compute this, like:
Code:
myData$lrr=0
for(i in 2:nrow(myData)){
  myData[i,]$lrr=(myData[i,]$value-myData[i-1,]$value)/myData[i-1,]$value
  
}
my question is, can I use a function/express to avoid for statement? like diff, cumsum.
I supposed there maybe some package can provide this function, but I don't know how to use it.
plz help me. because I need do a lot of finance computation, I will meet many computation like on year-on-year basis or link relative ratio, I want find a simple method.
 

Dason

Ambassador to the humans
#2
There is probably a way to make this a little cleaner but oh well

Code:
myData<-data.frame(year=c(2000,2001,2002,2003,2004),value=c(99,100,110,220,330))

myData$lrr <- with(myData, c(0, (value[-1] - value[-length(value)])/value[-length(value)]))