Sort data by row

前端 未结 1 1697
一向
一向 2020-12-21 09:52

I have a data frame like

Id  A B C D E F
a   1 2 9 4 7 6
b   4 5 1 3 6 10
c   1 6 0 3 4 5

I want a data frame like

Id
a  C         


        
相关标签:
1条回答
  • 2020-12-21 10:30

    Use order with apply, extracting the names in the process, like 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  a  C  E  F  D  B  A
    # 2  b  F  E  B  A  D  C
    # 3  c  B  F  E  D  A  C
    

    The result of apply needs to be transposed before it is recombined with the Id column.

    0 讨论(0)
提交回复
热议问题