2

I am working on an automation project in R. I want to order an input dataset (i.e. dataframe) by row values. No hardcoded values are allowed, since it's automated code. Reproducible example below:

data("mtcars")
groupBY <- c('cyl','carb')

Normally, I would go on and order with this:

mtcars_ordered <- mtcars[order(mtcars$cyl,mtcars$carb),]

or this:

mtcars_ordered <- mtcars[with(mtcars,order(cyl,carb)),]

but here I am in the 'automated world', no hardcoded values. Thus, I am looking for something like this:

mtcars_ordered_auto <- mtcars[with(mtcars,order(groupBY)),]

order() accepts vectors, that is subsets of the data frame, but I can't find a way to do it with R base only.

PS Eventually, I succeeded using fn$sqldf as suggested here, by writing:

groupBY_str <- paste(groupBY,collapse=',')
mtcars_ordered_sql <- fn$sqldf('select * from mtcars order by $groupBY_str').

Nonetheless, I am curious how to solve it with base R.

Nakx
  • 1,460
  • 1
  • 23
  • 32
Pittoro
  • 83
  • 6
  • 1
    Regarding the last line of code in the question with `fn$sqldf` note that you can't have underscores in names after the $. They will be regarded as terminating the name, not part of the name. You can either put back quotes around such a name or else use a different name having no special characters. – G. Grothendieck Apr 27 '20 at 15:03
  • I must admit, I edited my question in the browser, without rerunning it in R - I had no way to see this `Error: near "_str": syntax error`. Thanks! – Pittoro Apr 27 '20 at 21:06

1 Answers1

2

We can use do.call

mtcars_ordered_auto <- mtcars[do.call(order, mtcars[groupBY]),]

-checking with OP's output

identical(mtcars_ordered, mtcars_ordered_auto)
#[1] TRUE
akrun
  • 874,273
  • 37
  • 540
  • 662