loop through new.env() R (quantmod)

Viewed 154

I am new to R, loop and quantmod. I am trying to build a database that can be used be used easier for calculations (in a column format).

I was wondering how you would use a "loop" to automate the process below. I am simply binding two datasets together.

As you can see, I have bound two datasets together using rbind, Google and Apple stock prices. If I was to have 100 stocks, this process would take a very long time, thus I was wondering how I could automate this process?

library(quantmod)
tickers<- c("AAPL", "GOOG")
all<- new.env()
getSymbols(tickers,src="google", env = all, from = Sys.Date()-100, to = Sys.Date())

apple_share<- all$AAPL
colnames(apple_share)<- c("Open", "High", "Low", "Close", "Volume")
apple_share$Ticker<- rep(1, nrow(apple_share))

google_share<- all$GOOG
colnames(google_share)<- c("Open", "High", "Low", "Close", "Volume")
google_share$Ticker<- rep(2, nrow(google_share))

combined_data<- rbind(apple_share,google_share)

Many thanks,

Shoups

2 Answers
Related