R 关于为表独立排序每行

R 关于为表独立排序每行,r,R,目前,我有一个csv文件,如下所示 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 我把这张桌子读作

目前,我有一个csv文件,如下所示

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
我把这张桌子读作

test.matrix<-data.frame(read.table("test.csv",sep=",",header=T))
如何对每行进行排序?对于生成输出,如何将字符(例如,
grade-1
和数值(例如,
0.004461027
)放在单个条目中,例如,
grade-1:0.004461027

也许:

 res <- t( apply( dfrm[ 2:4], 1, 
                    function(row) paste0("grade-", 1:3, ":", rev(sort(row) ) ) ) )
我将drop equal FALSE放在这里,以保留第一个参数的dataframe类,因此结果将是data.frame。否则res对象是矩阵,dfrm[,“ID”]或dfrm$ID将是向量,
cbind
结果将是矩阵。

t(应用(DF,1,函数(x){
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"

两次计数的温度都正确。已修复。在函数中,温度
 cbind(dfrm[, "ID", drop=FALSE], res)
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"