Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain date column from xts object [duplicate]

Tags:

r

xts

quantmod

I used getSymbols to obtain stock data, and it returned something like this:

> require(quantmod)
> getSymbols(AAPL)
> head(AAPL)
           AAPL.Open AAPL.High AAPL.Low AAPL.Close
2007-01-03     86.29     86.58    81.90      83.80
2007-01-04     84.05     85.95    83.82      85.66
2007-01-05     85.77     86.20    84.40      85.05
2007-01-08     85.96     86.53    85.28      85.47
2007-01-09     86.45     92.98    85.15      92.57
2007-01-10     94.75     97.80    93.45      97.00
> str(AAPL)
An ‘xts’ object on 2007-01-03/2015-02-23 containing:
  Data: num [1:2049, 1:6] 86.3 84 85.8 86 86.5 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2015-02-24 17:12:45"

How do I obtain the dates? It seems the dates are not in the data. AAPL[1,1] returns:

           AAPL.Open
2009-01-02     85.88

And rownames(AAPL) returns NULL. What is going on here? How are the dates associated with the rest of object? How do I obtain the dates?

like image 333
NewbieDave Avatar asked Feb 15 '15 22:02

NewbieDave


1 Answers

getSymbols does not return a data.frame by default; it returns an xts object. xts objects do not have row names. They have an index attribute that you can access with the index function.

like image 196
Joshua Ulrich Avatar answered Nov 03 '22 05:11

Joshua Ulrich