Question

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!

Was it helpful?

Solution 2

You can do this with the package dplyr.

require(dplyr)     #install the package and load it into library

#group the data by "n" and calculate the cumulative sums of returns

df <- df %.% group_by(n) %.% mutate(cum_ret = cumsum(ret))     

Edit: to make sure the groups of n are sorted by date when calculating the cumulative returns, you can include arrange in the operation:

df <- df %.% arrange(n, dt) %.% group_by(n) %.% mutate(cum_ret = cumsum(ret))   

OTHER TIPS

> s <- split(df, df$n)
> ll <- lapply(s, function(x) cbind(x, cum_ret = cumprod(1+x$ret)))
> unsplit(ll, df$n)
#       n       dt   ret  cum_ret
# 1   IBM 20140407 0.006 1.006000
# 2   IBM 20140408 0.049 1.055294
# 3   IBM 20140409 0.069 1.128109
# ...
# 11 AAPL 20140407 0.068 1.068000
# 12 AAPL 20140408 0.002 1.070136
# 13 AAPL 20140409 0.042 1.115082
# ...
# 21 GOOG 20140407 0.082 1.082000
# 22 GOOG 20140408 0.041 1.126362
# 23 GOOG 20140409 0.036 1.166911
# ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top