R sort() 数据.frame
我有以下数据框
head(stockdatareturnpercent)
SPY DIA IWM SMH OIH
2001-04-02 8.1985485 7.8349806 7.935566 21.223832 13.975655
2001-05-01 -0.5621328 1.7198760 2.141846 -10.904936 -4.565291
2001-06-01 -2.6957979 -3.5838102 2.786250 4.671762 -23.241009
2001-07-02 -1.0248091 -0.1997433 -5.725078 -3.354391 -9.161594
2001-08-01 -6.1165559 -5.0276558 -2.461728 -6.218129 -13.956695
2001-09-04 -8.8900629 -12.2663267 -15.760037 -39.321172 -16.902913
实际上还有更多股票,但为了说明目的我不得不将其削减。每个月我都想知道表现最好到最差(或最差到最好)的人。我尝试了 sort() 函数,这就是我想到的。
N <- dim(stockdatareturnpercent)[1]
for (i in 1:N) {
s <- sort(stockdatareturnpercent[i,])
print(s)
}
UPS FDX XLP XLU XLV DIA IWM SPY XLE XLB XLI OIH XLK SMH MSFT
2001-04-02 0.6481585 0.93135 1.923136 4.712996 7.122751 7.83498 7.935566 8.198549 9.826701 10.13465 10.82522 13.97566 14.98789 21.22383 21.41436
SMH FDX OIH XLK XLE SPY XLU XLP DIA MSFT IWM UPS XLV XLB XLI
2001-05-01 -10.90494 -5.045544 -4.565291 -4.182041 -0.9492803 -0.5621328 0.6987724 1.457579 1.719876 2.088734 2.141846 3.73587 3.748309 3.774033 4.099748
OIH XLE XLI XLU XLP XLB DIA UPS SPY XLV FDX XLK IWM SMH MSFT
2001-06-01 -23.24101 -10.02403 -6.594324 -5.8602 -5.0532 -3.955192 -3.58381 -2.814685 -2.695798 -1.177474 0.4987542 1.935544 2.78625 4.671762 5.374764
MSFT OIH XLK IWM SMH XLV UPS XLE SPY XLU XLB XLI DIA FDX
2001-07-02 -9.793005 -9.161594 -7.17351 -5.725078 -3.354391 -2.016818 -1.692442 -1.159914 -1.024809 -0.9029407 -0.2723560 -0.2078283 -0.1997433 2.868898
XLP
2001-07-02 2.998604
这是一种非常低效且廉价的查看结果的方式。最好创建一个存储这些数据的对象。但是,如果我在 R 提示符中键入 's',我只会获得最后一行的值,因为 for 循环的每次后续迭代都会替换以前的数据。
我将非常感谢一些指导。谢谢您。
I have the following data frame
head(stockdatareturnpercent)
SPY DIA IWM SMH OIH
2001-04-02 8.1985485 7.8349806 7.935566 21.223832 13.975655
2001-05-01 -0.5621328 1.7198760 2.141846 -10.904936 -4.565291
2001-06-01 -2.6957979 -3.5838102 2.786250 4.671762 -23.241009
2001-07-02 -1.0248091 -0.1997433 -5.725078 -3.354391 -9.161594
2001-08-01 -6.1165559 -5.0276558 -2.461728 -6.218129 -13.956695
2001-09-04 -8.8900629 -12.2663267 -15.760037 -39.321172 -16.902913
Actually there are more stocks but for purposes of illustration I had to cut it down. In each month I want to know the best to worst (or worst to best) performers. I played around with the sort() function and this is what I came up with.
N <- dim(stockdatareturnpercent)[1]
for (i in 1:N) {
s <- sort(stockdatareturnpercent[i,])
print(s)
}
UPS FDX XLP XLU XLV DIA IWM SPY XLE XLB XLI OIH XLK SMH MSFT
2001-04-02 0.6481585 0.93135 1.923136 4.712996 7.122751 7.83498 7.935566 8.198549 9.826701 10.13465 10.82522 13.97566 14.98789 21.22383 21.41436
SMH FDX OIH XLK XLE SPY XLU XLP DIA MSFT IWM UPS XLV XLB XLI
2001-05-01 -10.90494 -5.045544 -4.565291 -4.182041 -0.9492803 -0.5621328 0.6987724 1.457579 1.719876 2.088734 2.141846 3.73587 3.748309 3.774033 4.099748
OIH XLE XLI XLU XLP XLB DIA UPS SPY XLV FDX XLK IWM SMH MSFT
2001-06-01 -23.24101 -10.02403 -6.594324 -5.8602 -5.0532 -3.955192 -3.58381 -2.814685 -2.695798 -1.177474 0.4987542 1.935544 2.78625 4.671762 5.374764
MSFT OIH XLK IWM SMH XLV UPS XLE SPY XLU XLB XLI DIA FDX
2001-07-02 -9.793005 -9.161594 -7.17351 -5.725078 -3.354391 -2.016818 -1.692442 -1.159914 -1.024809 -0.9029407 -0.2723560 -0.2078283 -0.1997433 2.868898
XLP
2001-07-02 2.998604
This is a very inefficient and cheap way to see the results. It would be nice to create an object that stores this data. However if I type 's' in the R prompt I only get the value of the last row as each subsequent iteration of the for loop replaces the previous data.
I would greatly appreciate some guidance. Thank you kindly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,请使用
order()
,因为sort()
在使用*apply
时会删除名称:使用
order
的结果id 矩阵中的 code> 还允许您执行以下操作:找出在给定时刻哪些是最好的。
如果你想使用循环,你可以使用列表。正如 Joshua 所说,你在每个循环中都覆盖 s 。首先初始化一个列表来存储结果。此循环给出的结果与上面使用
lapply()
的代码相同,但没有 id 矩阵。尽管使用 apply 有其他好处,但速度没有提高:我使用以下示例数据测试了代码(请在将来提供您自己的数据,使用此示例或例如
dput()
):Use
order()
for this, assort()
drops the names when using*apply
:Using the results of
order
in an id matrix also allows you to do eg :To find out wich ones were the best at a given moment.
If you want to use your loop, you could use lists. as Joshua said, you overwrite s in every loop. Initialize a list to store the results first. This loop gives the same results as the above code with
lapply()
, but without the id matrix. There's no gain in speed, although using apply has other benefits :I tested the code using following sample data (please provide your own in the future, using either this example or eg
dput()
) :使用原始代码将每个排序行保存在
list
中:直接使用
apply
对每行进行排序,但不保留元素名称:返回一个矩阵其中每一列都是排序后的行。然后转置:
如果您需要将结果作为 data.frame,则将其作为.data.frame:
最后,所有这些都在一行中
Using your original code to save each sorted row in a
list
:For a direct use of
apply
to sort each row, but does not preserve the element names:That returns a matrix where each column is the sorted row. Then transpose:
If you need the result as a data.frame, as.data.frame it:
Finally, all that in one line