I might not explain my questions clear in the title, apologize. Here is the question with the code, it will be clearer,
library(zoo);library(quantmod)
stockData <- new.env()#Make a new environment for quantmod to store data in
startDate = as.Date("2006-12-29") #Specify period of time we are interested in
endDate = as.Date("2012-12-31")
monthlyStartDate = as.Date("2007-01-01")
# tickers <- c("AAPL","GOOG", "IBM")
tickers <- c("AAPL","GOOG", "IBM", "MSFT", "INTC", "YHOO", "F", "GS", "UL")
# The tickers vector could be even larger, i.e. 50 stocks
stockData$AAPL.ret=diff(log(stockData$AAPL$AAPL.Adjusted)) # Compute log returns
stockData$GOOG.ret=diff(log(stockData$GOOG$GOOG.Adjusted)) # Compute log returns
stockData$IBM.ret=diff(log(stockData$IBM$IBM.Adjusted)) # Compute log returns
head(stockData$GOOG.ret)
head(stockData$GOOG$GOOG.Adjusted)
AAPLmonthly<-aggregate.zoo(stockData$AAPL.ret[2:nrow(stockData$AAPL$AAPL.Adjusted),],as.yearmon,sum)
GOOGmonthly<-aggregate.zoo(stockData$GOOG.ret[2:nrow(stockData$GOOG$GOOG.Adjusted),],as.yearmon,sum)
IBMmonthly<-aggregate.zoo(stockData$IBM.ret[2:nrow(stockData$IBM$IBM.Adjusted),],as.yearmon,sum)
head(AAPLmonthly)
stockret = cbind(AAPLmonthly, GOOGmonthly, IBMmonthly)
head(stockret)
The above code only used 3 tickers
as an example, I want to know how to do a loop
in R to make my whole vector of tickers into the stockret
zoo
object, could anybody help me? Much appreciated.
I am learning environment, just learnt the function is an closure, including the body, arguments and its environment, but I didn't know we could new an environment. So I got stuck here, stockData$AAPL.ret
how do I put the stockData$
in front of my each element while doing a loop to assign values? Should I use "["
to do this?
Besides, if I use the assign
function to do this, in the code
stockData$AAPL.ret=diff(log(stockData$AAPL$AAPL.Adjusted)) # Compute log returns
how to do this, I'm just confused about how to make this stockData$AAPL$AAPL.Adjusted
a more general argument in my assign
function, any example would be much appreciated!