I\'m trying to get a time series of returns for holding a certain asset for a specific time.
My dataframe looks like this:
Date Price
1998-01-01
You can use the ROC
function in the TTR package, or you can just create your own function.
> library(quantmod) # loads TTR
> getSymbols("SPY")
> tail(ROC(Cl(SPY),20))
SPY.Close
2010-12-09 0.01350383
2010-12-10 0.02307920
2010-12-13 0.03563051
2010-12-14 0.03792853
2010-12-15 0.04904805
2010-12-16 0.05432540
> tail(log(Cl(SPY)/lag(Cl(SPY),20)))
SPY.Close
2010-12-09 0.01350383
2010-12-10 0.02307920
2010-12-13 0.03563051
2010-12-14 0.03792853
2010-12-15 0.04904805
2010-12-16 0.05432540