I am using the following code to download the YTD AdjClose of SPY.
library(tseries)
AjdClose_SPY <- get.hist.quote("SPY", quote="Adj", start="2015-01-01", retclass="zoo")
Now, say I have a portfolio
portfolio <- c('SPY','AAPL','HD')
How would I be able to loop through "portfolio" and create a variable "AdjClose_" for each ticker in my portfolio? Thanks in advance!
setNames(lapply(portfolio, function(x) get.hist.quote(x, quote="Adj", start="2015-01-01", retclass="zoo")),paste0("AjdClose_",portfolio)). You'll get alistobject whose names are what you indicated. Don't create several variables; a single named list is much cleaner and easier to use.setNames(do.call(merge, lapply(portfolio, get.hist.quote, quote="Adj", start="2015-01-01", retclass="zoo")), portfolio)