[R-SIG-Finance] aggregate an xts by factors
Aaron Goldenberg
aaron at quantrisktrading.com
Tue Aug 4 01:48:47 CEST 2015
This seems like a simple issue but I cannot get it to work for some reason.
I have a time series object that has daily returns of several stocks that I
would like to aggregate by qualitative factor. In my toy example, for each
day, I would like to have two entries, one for the sum of the returns of my
two computer stocks and another for my financial stock. Then I would like
to calculate the cumulative sum of the returns for each factor. What am I
not doing correctly?
library(quantmod)
getSymbols("AAPL", src="yahoo", from="2015-07-01")
AAPL <- dailyReturn(AAPL$AAPL.Adjusted)
AAPL <- cbind(data.frame(AAPL), sector="Computer")
AAPL <- as.xts(AAPL)
getSymbols("GOOG", src="yahoo", from="2015-07-01")
GOOG <- dailyReturn(GOOG$GOOG.Adjusted)
GOOG <- cbind(data.frame(GOOG), sector="Computer")
GOOG <- as.xts(GOOG)
getSymbols("GS", src="yahoo", from="2015-07-01")
GS <- dailyReturn(GS$GS.Adjusted)
GS <- cbind(data.frame(GS), sector="Financial")
GS <- as.xts(GS)
combined <- rbind(AAPL, GOOG, GS)
#combined <- period.sum(combined$daily.returns, endpoints(combined,
on="days"))
combined <- aggregate(combined, by=combined$sector, sum)
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list