orderBy 随排序递减和递增的变化

Mat*_*ert 5 r

是否有按几列对 data.frame 进行排序的标准方法,但随着减少或增加的变化?例如,您可能希望按一个变量(递减)和下一个(递增)对 data.frame 进行排序。

有没有类似的东西:

mydf[ order(mydf$myvariable,mydf$myvariable2,decreasing=c(FALSE,TRUE)), ]
Run Code Online (Sandbox Code Playgroud)

mbq*_*mbq 5

快速解决方法:

 mydf[ order(mydf$myvariable,-mydf$myvariable2,decreasing=F), ]
Run Code Online (Sandbox Code Playgroud)

对于因子、字符串等:

 mydf[ order(mydf$myvariable,-xtfrm(mydf$myvariable2),decreasing=F), ]
Run Code Online (Sandbox Code Playgroud)


had*_*ley 5

library(plyr)
mydf[with(mydf, order(myvariable, desc(myvariable2)), ]

# Or, a little less typing:
arrange(mydf, myvariable, desc(myvariable2))
Run Code Online (Sandbox Code Playgroud)