One problem you're going to have is that the three downloads have different number of rows, so binding them all into a single data frame will fail.
The code below uses the last 1000 rows of each file (most recent), and does not use loops.
stocks <- c('AXP', 'VZ', 'V')
library('quantmod')
getSymbols(stocks)
prices=do.call(data.frame,
lapply(stocks,
function(s)tail(get(s)[,paste0(s,".Adjusted")],1000)))
colnames(prices)=stocks
head(prices)
# AXP VZ V
# 2010-02-08 34.70 21.72 80.58
# 2010-02-09 35.40 22.01 80.79
# 2010-02-10 35.60 22.10 81.27
# 2010-02-11 36.11 22.23 82.73
# 2010-02-12 36.23 22.15 82.38
# 2010-02-16 37.37 22.34 83.45
Working from the inside out, s
is the ticker (so, e.g., "AXP"
); get(s)
returns the object with that name, so AXP
; get(s)[,paste0(s,".Adjusted")]
is equivalent to AXP[,"AXP.Adjusted"]
; tail(...,1000)
returns the last 1000 rows of ...
. So when s="AXP", the function returns the last 1000 rows of AXP$AXP.Adjusted
.
lapply(...)
applies that function to each element in stocks.
do.call(data.frame,...)
invokes the data.frame function with the list of columns returned by lapply(...)
.