Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

orderBy with changes in decreasing and increasing sorting

Tags:

r

Is there a standard way of sorting a data.frame by several columns, but with changes in decrease or increase? For example, you may want to order a data.frame by one variable (decreasing) and by the next (increasing).

Is there something like:

mydf[ order(mydf$myvariable,mydf$myvariable2,decreasing=c(FALSE,TRUE)), ]
like image 599
Matt Bannert Avatar asked Jul 23 '10 08:07

Matt Bannert


2 Answers

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

# Or, a little less typing:
arrange(mydf, myvariable, desc(myvariable2))
like image 143
hadley Avatar answered Oct 13 '22 10:10

hadley


Quick workaround:

 mydf[ order(mydf$myvariable,-mydf$myvariable2,decreasing=F), ]

For factors, strings etc:

 mydf[ order(mydf$myvariable,-xtfrm(mydf$myvariable2),decreasing=F), ]
like image 20
mbq Avatar answered Oct 13 '22 10:10

mbq