Order data frame by two columns in R

后端 未结 2 794
自闭症患者
自闭症患者 2020-12-05 05:36

I\'m trying to reorder the rows of a data frame by two factors. For the first factor i\'m happy with the default ordering. For the second factor i\'d like to impose my own c

相关标签:
2条回答
  • 2020-12-05 06:10

    Reordering the factor levels:

    dat[with(dat, order(orange, as.integer(factor(apple, appleOrdered)))), ]
    
    0 讨论(0)
  • 2020-12-05 06:30

    Try using a factor with the levels in the desired order and the arrange function from plyr:

    dat$apple <- factor(dat$apple,levels=appleOrdered)
    arrange(dat,orange,apple)
    
    0 讨论(0)
提交回复
热议问题