访问 zoo 或 xts 索引 [英] Access zoo or xts index
本文介绍了访问 zoo 或 xts 索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用动物园对象,购买我的问题也适用于 xts 对象.在我看来,它是一个带有索引的单列向量.在我的情况下,索引是日期向量和一列向量我的数据.一切都很好,只是我想访问日期(从索引).
I am using zoo objects, buy my question also applies to xts objects. It looks to me like it is a one column vector with an index. In my case the index is the vector of dates and the one column vector my data. All is good except that I would like to access the dates (from the index).
例如我有以下结果:
ObjZoo <- structure(c(10, 20), .Dim = c(2L, 1L), index = c(14788, 14789),
class = "zoo", .Dimnames = list(NULL, "Data"))
unclass(ObjZoo)
# Data
# [1,] 10
# [2,] 20
# attr(,"index")
# [1] 14788 14789
我想在变量或向量中获取 14789
,但我不知道如何访问它.
I want to get 14789
in a variable or a vector, but I'm not sure how to access it.
推荐答案
从?zoo
的帮助中,有两种方便的方法可以访问zoo对象中的数据:
From the help for ?zoo
, there are two convenience methods to access the data in zoo objects:
coredata()
返回zoo对象中的数据index()
返回索引
coredata()
returns the data in the zoo objectindex()
returns the index
例如:
x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 9, 14) - 1
x <- zoo(rnorm(5), x.Date)
index(x)
[1] "2003-02-01" "2003-02-03" "2003-02-07" "2003-02-09" "2003-02-14"
coredata(x)
[1] -1.2487943 0.8911630 1.2713133 -0.1024638 0.2989194
这篇关于访问 zoo 或 xts 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文