Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a dataframe

Tags:

dataframe

r

I want to pass a character vector to a dataframe to sort it. How Can I do this? I have tried the following

headings.tosort <- c("mpg", "hp")
headings <- intersect(headings.tosort, names(mtcars))

mtcars[with(mtcars,order(headings)),]
mtcars[with(mtcars,order(mpg,hp)),]

I want to be able to create a vector with a list of headings to sort and then call it in a dataframe. The first expression does not work but the second one does so how do I get this to work?

like image 978
Kharoof Avatar asked Aug 02 '26 12:08

Kharoof


1 Answers

I maybe misunderstood your question, but I would do like that:

mtcars[do.call(order, mtcars[, headings]), ]
like image 94
sus_mlm Avatar answered Aug 04 '26 06:08

sus_mlm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!