从 xts 对象获取日期列 [英] Obtain date column from xts object
本文介绍了从 xts 对象获取日期列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 getSymbols
来获取股票数据,它返回如下内容:
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"
我如何获得日期?似乎日期不在数据中.AAPL[1,1]
返回:
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
并且 rownames(AAPL)
返回 NULL
.这里发生了什么?日期如何与对象的其余部分相关联?如何获取日期?
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?
推荐答案
getSymbols
默认不返回 data.frame
;它返回一个 xts 对象.xts 对象没有行名称.它们有一个 index 属性,您可以使用 index
函数访问该属性.
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.
这篇关于从 xts 对象获取日期列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文