r - Sort data by row -
i have data frame like
id b c d e f 1 2 9 4 7 6 b 4 5 1 3 6 10 c 1 6 0 3 4 5
i want data frame
id c e f d b #for a, c has highest value, e f , on...similarly other rows b f e b d c c b f e d c
basically, sorting each row of data frame first , replacing row values respective column names.
is there nice way this?
use order
apply
, extracting names
in process, this:
data.frame( mydf[1], t(apply(mydf[-1], 1, function(x) names(x)[order(x, decreasing = true)]))) # id x1 x2 x3 x4 x5 x6 # 1 c e f d b # 2 b f e b d c # 3 c b f e d c
the result of apply
needs t
ransposed before recombined id column.
Comments
Post a Comment