I am trying to find months in S&P 500 which yielded more than x%.
The following code find such instances:
getSymbols('^GSPC', from='2010-01-01')
G <- monthlyReturn(Cl(GSPC))
names(G) <- 'RET.M'
G[G$RET.M>.05]
Now, I would like to find out what is the yearly return of such instances?
I know I can calculate the yearly return as follow:
G$RET.Y <- yearlyReturn(Cl(GSPC))
But now, I want to query G$RET.Y to get the yearly return for months that yielded 5% or more.
Any suggestions?
You can merge them, fill backwards using
na.locfwithfromLast=TRUE, then subset as normalOr, you could format the index as a character representation of the year —
format(index(G[G$RET.M > 0.05]), "%Y")— then just subset the yearly data with that