10

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?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
NewbieDave
  • 1,199
  • 3
  • 12
  • 23
  • 1
    Why is this marked as duplicate @Joshua Ulrich? A person new to r wouldnt know what index is and would look for a question similar to this. The link to the "original" question itself has the word "index" in it. – Apurv May 22 '17 at 04:43
  • @Apurv I understand. Closing as a duplicate helps prevent people from providing similar answers to similar questions. Instead the answers will all be under one question. People can still search and find this question, then view the linked question. – Joshua Ulrich Aug 03 '20 at 18:33

1 Answers1

11

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.

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