Q:

Regarding sorting each row independently for a table

Currently, I have a csv file like the following one
ID      grade-1         grade-2         grade-3
 1    0.004461027   0.002740424 0.002955164
 2    0.055690775   0.045791653 0.17440305
 3    0.048901623   0.042439538 0.027306325
 4    0.20013265    0.0637944   0.081362503

I read table as 
test.matrix<-data.frame(read.table("test.csv",sep=",",header=T))

I would like to generate a new table where each row was sorted
ID      highest grade           the second grade           the third grade
1   grade-1:0.004461027 grade-3:0.002955164        grade-2:0.002740424  
2   grade-3:0.17440305      grade-1:0.055690775    grade-2:0.045791653  
3   grade-1:0.048901623 grade-2:0.042439538    grade-3:0.027306325
4   grade-1:0.20013265  grade-3:0.081362503        grade-2:0.0637944    

How can I sort each row? And for generating the output, how to put a character, e.g., grade-1 and numerical value,e.g., 0.004461027 in a single entry, e.g., grade-1:0.004461027?

A:

t(apply(DF,1,function(x) {
  temp <- sort(x[-1],decreasing=TRUE)
  res <- c(x[1],paste(names(temp),temp,sep=": "))
  names(res) <- c("ID",      "highest grade",           "the second grade",           "the third grade")
  res
                        }))

     ID  highest grade          the second grade       the third grade       
[1,] "1" "grade.1: 0.004461027" "grade.3: 0.002955164" "grade.2: 0.002740424"
[2,] "2" "grade.3: 0.17440305"  "grade.1: 0.055690775" "grade.2: 0.045791653"
[3,] "3" "grade.1: 0.048901623" "grade.2: 0.042439538" "grade.3: 0.027306325"
[4,] "4" "grade.1: 0.20013265"  "grade.3: 0.081362503" "grade.2: 0.0637944"

