目前,我有一个像以下一样的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))
我想生成一个新表,其中每行都已排序
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
如何对每一行进行排序?为了生成输出,如何在一个条目中放置一个字符,例如grade-1
和数值,例如0.004461027
,例如grade-1:0.004461027
?
答案 0 :(得分:2)
也许:
res <- t( apply( dfrm[ 2:4], 1,
function(row) paste0("grade-", 1:3, ":", rev(sort(row) ) ) ) )
R按列顺序返回矩阵结果,因此当函数应用于行时,您需要转置结果以获得序数“形状”。要返回ID值,请将ID绑定到ID:
cbind(dfrm[, "ID", drop=FALSE], res)
我把drop放在那里等于FALSE以保留第一个参数的dataframe类,因此结果将是data.frame。否则,res-object是一个矩阵,dfrm [,“ID”]或dfrm $ ID将是向量,cbind
结果将是一个矩阵。
答案 1 :(得分:1)
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"