How can I calculate the difference between different rows within different list? and different list have different dimensions. I use the code as follows
names(ri1)
[1] "Sedol" "code"  "ri"    "date"
ri1<-ri1[order(ri1$Sedol,ri1$date),]
sri<-split(ri1,ri1$Sedol)
ri1$r<-as.vector(sapply(seq_along(sri), function(x) diff(c(0, sri[[x]][,3]))))
however it shows the result
"Error in `$<-.data.frame`(`*tmp*`, "r", value = list(c(100, 0.00790000000000646,  : 
replacement has 1485 rows, data has 4687655"
for example
I have three lists
- date ri 1990 1 1991 2 1992 3
- date ri 1990 1 1991 2 1992 3 1993 4
- date ri 1990 1 1991 2
I want the results like
- date ri r 1990 1 0% 1991 2 100% 1992 3 100%
- date ri r 1990 1 0% 1991 2 100% 1992 3 100% 1993 4 100%
- date ri r 1990 1 0% 1991 2 100%
notice: r= r(t+1)/r(t)-1
 
     
     
    