如何返回与 xts 中排序列表的每个成员关联的日期

发布于 2024-12-05 17:39:54 字数 576 浏览 0 评论 0原文

我有一个 xts 对象“foo”,其中包含在特定时期内股票股价价值的前 6 个最大负百分比变化。使用 sort(foo) 会生成一个按日期排序的列表,如下所示,但我想根据值对列表进行排序。

sort(coredata(foo)) 为我提供了我期望的列表,但返回的值没有关联的索引日期值,如下所示。我想要一个格式为以下的列表:

2008-11-07 -0.150
2008-11-06 -0.145

etc

我觉得 index()which() 的某种组合可能有效,但无法产生任何有用的东西。任何指点都感激不尽。

sort(foo)

   [,1]
2008-10-08 -0.105
2008-10-16 -0.119
2008-10-27 -0.109
2008-11-06 -0.145
2008-11-07 -0.150
2008-12-12 -0.121

sort(coredata(foo))
[1] -0.150 -0.145 -0.121 -0.119 -0.109 -0.105

I have an xts object 'foo' containing the top 6 largest negative percentage changes in the value of the share price of a stock over a certain period. Using sort(foo) produces a list sorted by date, as shown below, but I want to sort the list based on the values.

sort(coredata(foo)) gives me the list I expect, but returns the values without the associated index date value, as shown below. I would like a list in the format:

2008-11-07 -0.150
2008-11-06 -0.145

etc

I feel that some combination of index() and which() might work but haven't been able to produce anything useful. Any pointers gratefully received.

sort(foo)

   [,1]
2008-10-08 -0.105
2008-10-16 -0.119
2008-10-27 -0.109
2008-11-06 -0.145
2008-11-07 -0.150
2008-12-12 -0.121

sort(coredata(foo))
[1] -0.150 -0.145 -0.121 -0.119 -0.109 -0.105

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

っ〆星空下的拥抱 2024-12-12 17:39:54

类似的东西会给你所需的顺序的日期:

 index(foo)[ order(coredata(foo)) ]

xts和zoo对象不喜欢乱序显示,所以我认为你需要将它们强制到一个不太“顺序”的类:

> foo3 <- as.data.frame(foo)
> foo3[order(foo3$V1), ]
[1] -0.150 -0.145 -0.121 -0.119 -0.109 -0.105
> foo3[order(foo3$V1), ,drop=FALSE]
               V1
2008-11-07 -0.150
2008-11-06 -0.145
2008-12-12 -0.121
2008-10-16 -0.119
2008-10-27 -0.109
2008-10-08 -0.105

Something along the lines of this would give you the dates in the desired order:

 index(foo)[ order(coredata(foo)) ]

xts and zoo objects do not like to be displayed out of order, so I think you would need to coerce them to a less "order"-ly class:

> foo3 <- as.data.frame(foo)
> foo3[order(foo3$V1), ]
[1] -0.150 -0.145 -0.121 -0.119 -0.109 -0.105
> foo3[order(foo3$V1), ,drop=FALSE]
               V1
2008-11-07 -0.150
2008-11-06 -0.145
2008-12-12 -0.121
2008-10-16 -0.119
2008-10-27 -0.109
2008-10-08 -0.105
巾帼英雄 2024-12-12 17:39:54

正如 @DWin 指出的,索引必须是有序的(zoo 代表“Z 的有序对象”),因此您不能取消 xts/zoo 对象的排序。

您可以使用 fmt 参数来 coredata.xts(如?coredata.xts中所述)与drop结合以获得一个命名向量,然后您可以根据需要对其进行排序。

sort(drop(coredata(x,fmt=TRUE)))
# 2008-11-07 2008-11-06 2008-12-12 2008-10-16 2008-10-27 2008-10-08 
#     -0.150     -0.145     -0.121     -0.119     -0.109     -0.105

As @DWin pointed out, the index must be ordered (zoo stands for "Z's ordered objects), so you can't un-order xts/zoo objects.

You can use the fmt argument to coredata.xts (as described in ?coredata.xts) combined with drop to get a named vector that you could then sort like you want.

sort(drop(coredata(x,fmt=TRUE)))
# 2008-11-07 2008-11-06 2008-12-12 2008-10-16 2008-10-27 2008-10-08 
#     -0.150     -0.145     -0.121     -0.119     -0.109     -0.105
最丧也最甜 2024-12-12 17:39:54

我认为 xtsprint 方法强加了日期排序,因此您无法通过单独子集来解决此问题:比较 foo[1:2]foo[2:1]

为了得到你想要的,你可以尝试,

ord <- order(foo)
dat <- data.frame(x=coredata(foo)[ord])
rownames(dat) <- index(foo)[ord]

但是这不是是一个xts对象。

I think the print method for xts imposes a date ordering, so you can not solve this by subsetting alone: compare foo[1:2] and foo[2:1].

To get what you want you could try,

ord <- order(foo)
dat <- data.frame(x=coredata(foo)[ord])
rownames(dat) <- index(foo)[ord]

This would not be an xts object however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文