R sort()data.frame [英] R sort() 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
其实有更多的股票,但为了说明的目的,我不得不削减。在每个月,我想知道最好的(最坏的或最差的)表演者。我使用sort()函数玩,这是我想出来的。
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
这是一种非常低效和便宜的方式来查看结果。创建一个存储这些数据的对象是很好的。但是,如果我在R提示符中输入's',那么只会得到最后一行的值,因为for循环的每个后续迭代将替换以前的数据。
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.
推荐答案
使用 order()
sort()
在使用 * apply
时删除名称:
Use order()
for this, as sort()
drops the names when using *apply
:
id <- t(apply(Data,1,order))
lapply(1:nrow(id),function(i)Data[i,id[i,]])
使用订单的结果
在一个id矩阵中也允许你做例如:
Using the results of order
in an id matrix also allows you to do eg :
matrix(names(Data)[id],ncol=ncol(Data))
[,1] [,2] [,3] [,4] [,5]
[1,] "DIA" "IWM" "SPY" "OIH" "SMH"
[2,] "SMH" "OIH" "SPY" "DIA" "IWM"
[3,] "OIH" "DIA" "SPY" "IWM" "SMH"
[4,] "OIH" "IWM" "SMH" "SPY" "DIA"
[5,] "OIH" "SMH" "SPY" "DIA" "IWM"
[6,] "SMH" "OIH" "IWM" "DIA" "SPY"
在某一时刻最好的。
如果要使用循环,可以使用列表。正如约书亚所说,你在每个循环中都会覆盖。初始化列表以首先存储结果。这个循环与 lapply()
的上述代码相同,但没有id矩阵。尽管使用apply还有其他好处:
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 :
N <- nrow(Data)
s <- vector("list",N)
for (i in 1:N) {
s[[i]] <- sort(Data[i,])
}
我使用以下示例数据测试了代码(请在以后使用本示例或例如 dput()
):
I tested the code using following sample data (please provide your own in the future, using either this example or eg dput()
) :
zz <- textConnection(" SPY DIA IWM SMH OIH
8.1985485 7.8349806 7.935566 21.223832 13.975655
-0.5621328 1.7198760 2.141846 -10.904936 -4.565291
-2.6957979 -3.5838102 2.786250 4.671762 -23.241009
-1.0248091 -0.1997433 -5.725078 -3.354391 -9.161594
-6.1165559 -5.0276558 -2.461728 -6.218129 -13.956695
-8.8900629 -12.2663267 -15.760037 -39.321172 -16.902913 ")
Data <- read.table(zz,header=T)
close(zz)
这篇关于R sort()data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!