0
$\begingroup$

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

$\endgroup$
2
  • 2
    $\begingroup$ This question belongs on stackoverflow.com $\endgroup$ Feb 16, 2014 at 16:01
  • $\begingroup$ thank you Josh, I placed the question now on the aforementioned website as per your suggestion. $\endgroup$ Feb 17, 2014 at 3:29

1 Answer 1

2
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ 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> $\endgroup$ Feb 17, 2014 at 3:04
  • $\begingroup$ @EdgarSMartinez What is the name of the object in your environment? $\endgroup$ Feb 17, 2014 at 3:58
  • $\begingroup$ the environment is called stockData $\endgroup$ Feb 17, 2014 at 4:03
  • $\begingroup$ @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. $\endgroup$ Feb 17, 2014 at 7:56

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