I'm fiddling with this now for a while, but can't find reasonable solution.
I would like to sort in descending order all columns of data.frame.
Sample data for instance:
CustomData <- data.frame(Value1=rnorm(100,1,2), Value2=rnorm(100,2,3),
Value3=rexp(100,5), Value4=rexp(100,2))
Works for one column:
CustomData[order(CustomData$Value1, decreasing=FALSE), ]
How sort all the columns data in decreasing/increasing order in reasonable manner? Thx.
I have also tried something like this as posted elsewhere, but doesn't work as stated.
CustomData[do.call(order, as.list(CustomData)),]
We can sort it by using the dataframe. sort_index() function. Alternatively, you can sort the index in descending order by passing in the ascending=False the argument in the function above.
Sorting Your DataFrame on a Single Column. 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.
Using do.call is much faster.
For ascending order.
CustomData[do.call(order, CustomData),]
For decreasing order the syntax is a bit more elaborate because we have to pass the 'decreasing' argument.
CustomData[do.call(order, c(CustomData, list(decreasing=TRUE))),]
CD.sorted <- apply(CustomData, 2, sort, decreasing=F)
#2 == column, 1 == row
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