For this sample data.frame,
df <- data.frame(var1=c("b","a","b","a","a","b"),
var2=c("l","l","k","k","l","k"),
var3=c("t","t","x","t","x","x"),
var4=c(5,3,3,5,5,3),
stringsAsFactors=F)
Unsorted
var1 var2 var3 var4
1 b l t 5
2 a l t 3
3 b k x 3
4 a k t 5
5 a l x 5
6 b k x 3
I would like to sort on three columns 'var2', 'var3' and 'var4' in this order simultaneously. One column ascending and another two descending. Column names to sort are stored in variables.
sort_asc <- "var2"
sort_desc <- c("var3","var4")
What's the best way to do this in base R?
Updated details
This is the output if sorted ascending by 'var2' first (step 1) and then descending by 'var3' and 'var4' (as step 2).
var1 var2 var3 var4
a l x 5
b k x 3
b k x 3
a k t 5
b l t 5
a l t 3
But what I am looking for is doing all three sort at the same time to get this:
var1 var2 var3 var4
b k x 3
b k x 3
a k t 5
a l x 5
b l t 5
a l t 3
'var2' is ascending (k,l), within k and within l, 'var3' is descending, and similarly 'var4' is descending
To clarify, how this question is different from other data.frame ordering questions...
- ordering on multiple columns
- column names to order on are stored in variables
- different ordering directions (asc,desc)
- ordering is not step-wise (one sort after another) but rather simultaneous (all selected columns at same time)
- using base R, not dplyr