对列进行排名并将得分最高的列放在第一位,依此类推

San*_*euw 3 r dataframe

假设我有一个非常大的data.frame包含每列的分数.

例如:

MA0001.1 AGL3 MA0003.1 TFAP2A MA0004.1 Arnt  MA0005.1 AG   MA0006.1 Arnt::Ahr
7.789524e-09  0.4012127249    3.771518e-03   1.892011e-06  0.002733200
5.032498e-07  0.0001873801    9.947449e-05   3.284222e-05  0.001367041
1.194487e-06  0.0009357406    6.943634e-05   1.589373e-05  0.002551519
4.833494e-06  0.0150703600    1.003488e-04   1.197928e-03  0.001431416
6.865040e-05  0.0000732607    3.857193e-04   5.388744e-03  0.001363706
Run Code Online (Sandbox Code Playgroud)

R data.frame:

testfr<-structure(list(`MA0001.1 AGL3` = c(7.78952366977488e-09, 5.03249791215203e-07, 
1.19448739380034e-06, 4.83349413748598e-06, 6.86504034402563e-05
), `MA0003.1 TFAP2A` = c(0.401212724871542, 0.000187380067026448, 
0.000935740631438077, 0.0150703600158589, 7.32607018758816e-05
), `MA0004.1 Arnt` = c(0.00377151826447817, 9.94744903768433e-05, 
6.94363387424972e-05, 0.000100348764966112, 0.00038571926458373
), `MA0005.1 AG` = c(1.89201084302835e-06, 3.2842217133538e-05, 
1.58937284554136e-05, 0.00119792816070882, 0.00538874414923338
), `MA0006.1 Arnt::Ahr` = c(0.00273319966783363, 0.00136704060025893, 
0.00255151921946167, 0.00143141576426544, 0.00136370552325235
)), .Names = c("MA0001.1 AGL3", "MA0003.1 TFAP2A", "MA0004.1 Arnt", 
"MA0005.1 AG", "MA0006.1 Arnt::Ahr"), class = "data.frame", row.names = c(4L, 
2L, 5L, 1L, 3L))
Run Code Online (Sandbox Code Playgroud)

现在我想选择具有最高值的列并将该列放在第一位.因此,1列的值应保持低于相同的列名称,整个列应按等级移动.

我尝试了以下方法:

ranked<-unlist(lapply(testfr,rank))
testranked<-testfr[ranked, ]
Run Code Online (Sandbox Code Playgroud)

这产生了一个2259obs*459vars的数据框,而原始的是5*459.

请注意,testfr是一个data.frame派生自一个函数,它将序列评分为矩阵列表!并将该分数返回到data.frame,其中行是序列,列是矩阵.

我知道我做索引或不列出时出错了,但我没有任何线索如何解决这个问题.任何帮助表示赞赏.

Aru*_*run 7

这个怎么样?

> testfr[rev(order(sapply(testfr, max, na.rm = TRUE)))]
Run Code Online (Sandbox Code Playgroud)

分解:

sapply(test.fr, max, na.rm = TRUE) # get max of each column (after removing NA)
order(.) # get the order of these values in increasing order
rev(.)   # get the reverse order so that highest value index stays first
testfr[.] # get the columns in this order back
Run Code Online (Sandbox Code Playgroud)