I have a zoo object in R of percent changes. I would like to compound those percent changes starting from 1 or 100 and see if we have compounded expansion/decay.
Lines <- "obs date pctchng comgrowth
1 2010-10-04 NA 100
2 2010-10-01 .15 NA
3 2010-09-30 .14 NA
4 2010-09-29 -.05 NA
5 2010-09-28 -.12 NA
6 2010-09-27 .07 NA
7 2010-09-24 -.15 NA
8 2010-09-23 .186 NA
9 2010-09-22 .01 NA
10 2010-09-21 .03 NA
11 2010-09-20 -.03 NA"
data <- read.zoo(textConnection(Lines), header=TRUE, index=2)
startobs <- 1
for (i in 1:100) {
data[startobs+i,"comgrowth"] <- data[startobs+i-1,"comgrowth"] *
(1+data[startobs+i,"pctchng"])
}
I am simplifying the data to make it simple to show but this is the idea I am trying to do. I realized the problem is when adding/subtracting/dividing/multiplying. For some reason I can say data[startobs+i,"comgrowth"]=data[startobs+i-1,"comgrowth"]
but...
I cannot say data[startobs+i,"comgrowth"]=data[startobs+i-1,"comgrowth"]+data[startobs+i-2,"comgrowth"]
. R won't let me add the +/-/* etc. and gives me this error
Error in NextMethod("[<-") : replacement has length zero
It's a syntatical problem, pure and simple. Does anyone know what I am doing wrong?