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

R 对列进行排序,并将得分最高的列放在第一位,依此类推,r,dataframe,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.001367

假设我有一个非常大的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
R数据帧:

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))
testfr这个怎么样

> testfr[rev(order(sapply(testfr, max, na.rm = TRUE)))]
细分:

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

为了可读性,我会使用
apply

testfr[order(apply(testfr, 2, max, na.rm = TRUE),decreasing=T)]

我在这里为每个边距、列应用max,然后按降序对列进行排序。

nice chaining of functions:)@Arun确实是nice chaining of functions。但当将其应用于测试数据集时,我仍然认为输出是不正确的。因为列的最大值不会改变。并且旧框架列.names的顺序与新框架相同,只对值进行了排序,而不对columns@SanderVanderZeeuw,您的data.frame中有
NA
值吗?@SanderVanderZeeuw,现在试试。在
]
之前有一个
,这就是问题所在。我还确保解释了
NA
值。@Arun谢谢你的修复和解释,我将你的答案标记为正确答案!但在“应用”之前,它会在内部转换为矩阵。我不明白为什么要使用
apply
。。。感谢您按顺序选择
discreating=TRUE
。@agstudy感谢您的另一个选项!我来看看。但是阿伦是第一个,所以我会接受他的答案。你能给我解释一下你为什么选择降价吗?因为我想选择最高的scores@Arun我认为这里有一个data.frame而不是列表,所以使用
apply
对我来说是很自然的。@SanderVanderZeeuw
discreating=TRUE
表示从高到低的值。因为你想要最高的分数。@agstudy这样做:
df
testfr[order(apply(testfr, 2, max, na.rm = TRUE),decreasing=T)]