I am trying to create a column in a data frame that has the cumulative returns. The data frame has multiple stocks, and is indicated by a column, "n". I would like to calculate the cumulative return for each of these stocks without the need to split the data frame into the number of stocks. Please see this example:
library("lubridate")
n=c("IBM","IBM","IBM","IBM","IBM","IBM","IBM","IBM","IBM","IBM",
"AAPL","AAPL","AAPL","AAPL","AAPL","AAPL","AAPL","AAPL","AAPL","AAPL",
"GOOG","GOOG","GOOG","GOOG","GOOG","GOOG","GOOG","GOOG","GOOG","GOOG"
)
dt=c("20140407","20140408","20140409","20140410","20140411",
"20140414","20140415","20140416","20140417","20140418",
"20140407","20140408","20140409","20140410","20140411",
"20140414","20140415","20140416","20140417","20140418",
"20140407","20140408","20140409","20140410","20140411",
"20140414","20140415","20140416","20140417","20140418"
)
ret=c(0.006,0.049,0.069,0.068,0.062,0.035,0.001,0.048,0.034,0.025,
0.068,0.002,0.042,0.036,0.01,0.006,0.074,0.021,0.005,0.028,
0.082,0.041,0.036,0.083,0.012,0.031,0.061,0.032,0.061,0.041
)
df=data.frame(n,dt,ret)
df$dt<-ymd(as.character(df$dt))
df$cum_ret <-cumprod(1+df$ret)
As you can see, this data set has 3 stocks. I would like to change df$cum_ret so that it's the cumulative return for each stock (noted in column "n")
Does anyone have any thoughts? Thank you so much!