Arthur
Arthur

Reputation: 33

R data.frame order

I want to order data.frame by different columns

    xx = mtcars
    oi1 = order(xx$mpg,xx$cyl)
    oi2 = order(xx$cyl,xx$disp,xx$hp)

etc,

Is there a way I can pass a list of columns to order?

something like:

   my.order = c('disp','wt','carb')
   oi = order(xx,my.order)

thank you.

Upvotes: 0

Views: 97

Answers (1)

MrFlick
MrFlick

Reputation: 206207

Here you could use do.call to make parameters from a list. For example

my.order = c('disp','wt','carb')
do.call("order", mtcars[my.order])

Upvotes: 1

Related Questions