キーコマンドは、apply
,order
、一部の行の並び替えです。
##Create some dummy data
##You should always try and include some test data in your questions
R> dd = as.data.frame(matrix(rnorm(80), ncol=8))
R> dd = cbind(GENE = LETTERS[1:10], dd)
R> head(dd, 2)
GENE V1 V2 V3 V4 V5 V6 V7 V8
1 A 1.693 1.2977 1.2220 0.4877 -1.7076 1.7796 0.7980 0.08643
2 B 1.987 0.1545 -0.2173 -0.5959 0.7274 0.2757 -0.5391 0.56054
##Work out the sd for columns 2 to 9 using apply
##Use "order" to reorder the rows
R> dd1 = dd[order(apply(dd[,2:9], 1, sd)),]
##Check the new order
R> apply(dd1[,2:9], 1, sd)
8 7 5 9 2 1 4 6 10 3
0.5197 0.7128 0.8149 0.8210 0.8624 0.8808 0.9804 1.2058 1.5086 1.6191
R> head(dd1, 2)
GENE V1 V2 V3 V4 V5 V6 V7 V8
8 H -0.3869 0.6206 0.279 -0.3867 -0.4915 -1.0979 -0.07696 -0.09097
7 G -1.2966 -1.1279 -1.082 -0.4739 0.2717 -0.1365 0.38614 0.38445