Reordering dcast data frame

后端 未结 2 1240
半阙折子戏
半阙折子戏 2020-12-20 18:33

Is it possible to reorder the columns of data frame which is a result of dcast() call E.x.

Given the data:

> dput(copyOfRes)
structure(list(docNa         


        
相关标签:
2条回答
  • 2020-12-20 18:37

    You can use reorder here with rev

    dcast(copyOfRes, docName ~ reorder(week_number,rev(week_number)), length)
    Using week_number as value column: use value.var to override.
      docName 11 2
    1    doc1 10 0
    2    doc2  1 1
    
    0 讨论(0)
  • 2020-12-20 18:49

    You can use factor() inside dcast() set appropriate order of levels.

     dcast(copyOfRes, 
       docName ~ factor(week_number,levels=unique(week_number)), length)
          docName 11 2
        1    doc1 10 0
        2    doc2  1 1
    
    0 讨论(0)
提交回复
热议问题