If we need only sortrow by row, use applyc MARGIN=1and assign the output back to the original columns after transferring the output.
df1[-1] <- t(apply(df1[-1], 1,
FUN=function(x) sort(x, decreasing=TRUE)))
df1
NOTE. But we may need to change the column names, as sorting by row gives new sorted values.
apply , , Map , cbind, .
nMat <- `dim<-`(names(df1)[-1][t(apply(df1[-1], 1,
order, decreasing=TRUE))], dim(df1[-1]))
vMat <- t(apply(df1[-1], 1, sort, decreasing=TRUE))
cbind(df1[1], data.frame(Map(cbind, as.data.frame(nMat,
stringsAsFactors=FALSE), as.data.frame(vMat))))
data.table. melt "" "long", "Name", we order "" "i", Data.table(.SD), ('N'), dcast 'long' 'wide'.
library(data.table)
dcast(melt(setDT(df1), id.var='Name')[order(-value),
.SD, Name][, N:=paste0("Col", 1:.N) , .(Name)],
Name~N, value.var=c("variable", "value"))
# Name variable_Col1 variable_Col2 variable_Col3 value_Col1 value_Col2 value_Col3
#1: John French Math English 86 78 56
#2: Sam Math French English 97 86 79
#3: Viru English Math French 93 44 34