Order a data frame programmatically using a character vector of column names
dataframe, r, sorting
Solution
`order` expects multiple ordering variables as separate arguments, which is unfortunate in your case but suggests a direct solution: use `do.call`:
df[do.call(order, df[, sortvar]), ]
In case you’re unfamiliar with `do.call`: it constructs and executes a call programmatically. The following two statements are equivalent:
fun(arg1, arg2, …)
do.call(fun, list(arg1, arg2, …))
Problem
I am trying to order a data frame on multiple columns. And the column names are passed through variable, i.e. a character vector. ``` df <- data.frame(var1 = c("b","a","b","a"), var2 = c("l","l","k","k"), var3 = c("t","w","x","t")) var1 var2 var3 1 b l t 2 a l w 3 b k x 4 a k t ``` Sorting on one column using a variable ``` sortvar <- "var1" df[order(df[ , sortvar]),] var1 var2 var3 2 a l w 4 a k t 1 b l t 3 b k x ``` Now, if I want to order by two columns, the above solution does not work. ``` sortvar <- c("var1", "var2") df[order(df[, sortvar]), ] #does not work ``` I can manually order with column names: ``` df[with(df, order(var1, var2)),] var1 var2 var3 4 a k t 2 a l w 3 b k x 1 b l t ``` But, how do I order the data frame dynamically on multiple columns using a variable with column names? I am aware of the `plyr` and `dplyr` `arrange` function, but I want to use `base` R here.