Take the 2-minute tour ×
Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. It's 100% free, no registration required.

I use getSymbols to download historical information over an environment with 200 tickers but I cannot seem to keep the date information from becoming an index. I tried to specify return.class to "ts" and "zoo" also but no luck...

I also used get.hist.quote with similar success...

BUT the only thing that works is

xom <- read.csv("http://ichart.yahoo.com/table.csv?s=xom&a=11&b=31&c=2012", stringsAsFactors=F)


# The first column contains dates.  The as.Date
# function can convert strings into Date objects.
#
    xom_dates <- as.Date(xom[,1])

is there ANY way to preferably use the getSymbols to format one column with the dates as as.Date as above?? Help

share|improve this question

closed as off-topic by Joshua Ulrich, user508, olaker Feb 22 at 3:50

  • This question does not appear to be about quantitative finance within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
This question belongs on stackoverflow.com –  Joshua Ulrich Feb 16 at 16:01
    
thank you Josh, I placed the question now on the aforementioned website as per your suggestion. –  Edgar S Martinez Feb 17 at 3:29

1 Answer 1

You cannot add a date column to an object returned by getSymbols or get.hist.quote. These function return matrices. Matrices can only store data of the same type, in this case the matrices contain double values (real numbers).

You can add a column of class Date to the objects if you transform them into a data frame:

For getSymbols:

library(quantmod)
getSymbols('IBM', from = "2014-02-10")

IBM
#            IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
# 2014-02-10   176.97   177.65  176.25    177.14    3540400       177.14
# 2014-02-11   176.81   180.39  176.80    179.70    4647300       179.70
# 2014-02-12   179.52   181.25  179.26    180.24    3983000       180.24
# 2014-02-13   178.22   182.36  177.86    181.84    4482000       181.84
# 2014-02-14   181.26   184.43  180.99    183.69    4659900       183.69

dat1 <- as.data.frame(IBM)
dat1$date <- time(IBM)

dat1
#            IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume       date
# 2014-02-10   176.97   177.65  176.25    177.14    3540400 2014-02-10
# 2014-02-11   176.81   180.39  176.80    179.70    4647300 2014-02-11
# 2014-02-12   179.52   181.25  179.26    180.24    3983000 2014-02-12
# 2014-02-13   178.22   182.36  177.86    181.84    4482000 2014-02-13
# 2014-02-14   181.26   184.43  180.99    183.69    4659900 2014-02-14

The approach is similar for get.hist.quote:

library(tseries) 
res <- get.hist.quote(instrument = "ibm",  start = "2014-02-10")

res
#              Open   High    Low  Close
# 2014-02-10 176.97 177.65 176.25 177.14
# 2014-02-11 176.81 180.39 176.80 179.70
# 2014-02-12 179.52 181.25 179.26 180.24
# 2014-02-13 178.22 182.36 177.86 181.84
# 2014-02-14 181.26 184.43 180.99 183.69

dat2 <- as.data.frame(res)
dat2$date <- time(res)

dat2
#              Open   High    Low  Close       date
# 2014-02-10 176.97 177.65 176.25 177.14 2014-02-10
# 2014-02-11 176.81 180.39 176.80 179.70 2014-02-11
# 2014-02-12 179.52 181.25 179.26 180.24 2014-02-12
# 2014-02-13 178.22 182.36 177.86 181.84 2014-02-13
# 2014-02-14 181.26 184.43 180.99 183.69 2014-02-14
share|improve this answer
    
Thank you Sven!! –  Edgar S Martinez Feb 17 at 2:29
    
I just tried and on an individual basis it works but since my information is being stored in an environment (stockData), this approach is not being accepted. The error message is: Downloading PBR Attempt: 1 / 5 <simpleError in as.data.frame.default(stockData): cannot coerce class ""environment"" to a data.frame> –  Edgar S Martinez Feb 17 at 3:04
    
@EdgarSMartinez What is the name of the object in your environment? –  Sven Hohenstein Feb 17 at 3:58
    
the environment is called stockData –  Edgar S Martinez Feb 17 at 4:03
    
@EdgarSMartinez You can try stockData2 <- eapply(stockData, as.data.frame). This will transform all objects in this environment into data frames. The result is stored in the list stockData2. –  Sven Hohenstein Feb 17 at 7:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.