4

I am completely new to R and I am learning how to program in R to get historical stock index data. I am planning to build an daily update code to update historic index data. I use a data environment call "indexData" to store the data as a xts. But unfortunately, the rbind or merge does not support data environments. They only support objects. I am wondering whether there are any workarounds or packages that I can used to solve this. My code is the following:

  indexData<-new.env()
  startDate<-"2013-11-02"
  getSymbols(Symbols=indexList,src='yahoo',from=startDate,to="2013-11-12",env=indexData)
  startDate<-(end(indexData$FTSE)+1)
  NewIndexData<-new.env()
  getSymbols(Symbols=indexList,src='yahoo',from=startDate,env=NewIndexData)
  rbind(indexData,NewIndexData) #it does not work for data environments

Much appreciate for any suggestions!

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418

1 Answers1

3

If you use auto.assign=FALSE, you get explicit variables -- and those you can extend as shown below.

First we get two IBM price series:

R> IBM <- getSymbols("IBM",from="2013-01-01",to="2013-12-01",auto.assign=FALSE) 
R> IBM2 <- getSymbols("IBM",from="2013-12-02",to="2013-12-28",auto.assign=FALSE) 

Then we check their dates:

R> c(start(IBM), end(IBM))
[1] "2013-01-02" "2013-11-29"
R> c(start(IBM2), end(IBM2))
[1] "2013-12-02" "2013-12-27"

Finally, we merge them and check the dates of the combined series:

R> allIBM <- merge(IBM, IBM2)
R> c(start(allIBM), end(allIBM))
[1] "2013-01-02" "2013-12-27"
R> 

You can approximate this by pulling distinct symbols out of the two environments but I think that is harder for you as someone beginning in R. So my recommendation would be to not work with environments -- see about lists instead.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725