如何返回与 xts 中排序列表的每个成员关联的日期
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类似的东西会给你所需的顺序的日期:
xts和zoo对象不喜欢乱序显示,所以我认为你需要将它们强制到一个不太“顺序”的类:
Something along the lines of this would give you the dates in the desired order:
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:
正如 @DWin 指出的,索引必须是有序的(zoo 代表“Z 的有序对象”),因此您不能取消 xts/zoo 对象的排序。
您可以使用
fmt
参数来coredata.xts
(如?coredata.xts
中所述)与drop
结合以获得一个命名向量,然后您可以根据需要对其进行排序。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 tocoredata.xts
(as described in?coredata.xts
) combined withdrop
to get a named vector that you could then sort like you want.我认为
xts
的print
方法强加了日期排序,因此您无法通过单独子集来解决此问题:比较foo[1:2]
和foo[2:1]
。为了得到你想要的,你可以尝试,
但是这不是是一个
xts
对象。I think the
print
method forxts
imposes a date ordering, so you can not solve this by subsetting alone: comparefoo[1:2]
andfoo[2:1]
.To get what you want you could try,
This would not be an
xts
object however.