Obtain date column from xts object [duplicate]

百般思念 提交于 2019-11-28 03:49:18

问题


This question already has an answer here:

  • Access zoo or xts index 2 answers

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?


回答1:


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.



来源:https://stackoverflow.com/questions/28532019/obtain-date-column-from-xts-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!