OrderBy with changes to decrease and increase sorting

Is there a standard way to sort data.frame across multiple columns, but with changes to decrease or increase? For example, you can order the data.frame file by one variable (decreasing) and the next (increasing).

Is there something like:

mydf[ order(mydf$myvariable,mydf$myvariable2,decreasing=c(FALSE,TRUE)), ] 
+4
source share
3 answers
 library(plyr) mydf[with(mydf, order(myvariable, desc(myvariable2)), ] # Or, a little less typing: arrange(mydf, myvariable, desc(myvariable2)) 
+3
source

Quick workaround:

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

For factors, rows, etc .:

  mydf[ order(mydf$myvariable,-xtfrm(mydf$myvariable2),decreasing=F), ] 
+3
source
 library(Deducer) sort(mtcars,by = ~ cyl - mpg) 
0
source

Source: https://habr.com/ru/post/1316634/


All Articles