I want to sort a data.frame
by multiple columns, ideally using base R without any external packages (though if necessary, so be it). Having read How to sort a dataframe by column(s)?, I know I can accomplish this with the order()
function as long as I either:
But what if I only have one vector containing multiple column names, of length that's unknown in advance?
Say the vector is called sortnames
.
data[order(data[, sortnames]), ]
won't work, because order()
treats that as a single sorting argument.
data[order(data[, sortnames[1]], data[, sortnames[2]], ...), ]
will work if and only if I specify the exact correct number of sortname values, which I won't know in advance.
Things I've looked at but not been totally happy with:
eval(parse(text=paste("data[with(data, order(", paste(sortnames, collapse=","), ")), ]")))
. Maybe this is fine, but I've seen plenty of hate for using eval()
, so asking for alternatives seemed worthwhile.Deducer
library to do this with sortData()
, but like I said, I'd rather avoid using external packages. If I'm being too stubborn about not using external packages, let me know. I'll get over it. All ideas appreciated in advance!
You can sort pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True.
To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order. It does not modify the original DataFrame.
Group By Multiple Columns in R using dplyrUse group_by() function in R to group the rows in DataFrame by multiple columns (two or more), to use this function, you have to install dplyr first using install. packages('dplyr') and load it using library(dplyr) . All functions in dplyr package take data.
To sort a DataFrame based on column names in descending Order, we can call sort_index() on the DataFrame object with argument axis=1 and ascending=False i.e.
You can use do.call
:
data<-data.frame(a=rnorm(10),b=rnorm(10))
data<-data.frame(a=rnorm(10),b=rnorm(10),c=rnorm(10))
sortnames <- c("a", "b")
data[do.call("order", data[sortnames]), ]
This trick is useful when you want to pass multiple arguments to a function and these arguments are in convenient named list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With