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
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: