1

I have looked through all the posts i could find on dplyr::arrange() or order() argument lengths differ errors, but have not found an explanation.

Im trying to make a function best() that can return the lowest rated value from a dataframe of hospital outcomes (dfout). When i copy the code straight into R it runs without an error, returning the hospital name with the lowest mortality rate. Only when i call it as a function does it say "Error in order(State, outcome, Hospital) : argument lengths differ"

The function: (note i used capitalized names for colnames and non capitalized for function variables)

best <- function(state, outcome){

colnames(dfout) <- c("Hospital", "State", "Heartattack", "Heartfailure", "Pneumonia")

##Return hospital name with lowest 30 day mortality rate

arranged <- arrange(dfout, State, outcome, Hospital)    ## arrange hospitals by state, mortality rate in the specified outcome in best() and alphabetically for the ties.
arranged1 <- arranged[arranged$State == state,]         ## take the part of the ordered list where state = the state specified in best()
arranged1$Hospital[1]

Now if i call best("TX", Heartattack) i get "Error in order(State, outcome, Hospital) : argument lengths differ", but if i simply run the code and replace state and outcome with "TX" and Heartattack i get a hospital, like this

##Return hospital name with lowest 30 day mortality rate

arranged <- arrange(dfout, State, Heartattack, Hospital)    ## arrange hospitals by state, mortality rate in the specified outcome in best() and alphabetically for the ties.
arranged1 <- arranged[arranged$State == "TX",]         ## take the part of the ordered list where state = the state specified in best()
arranged1$Hospital[1]
[1] "CYPRESS FAIRBANKS MEDICAL CENTER"

My question is really: how can the function not work, when copying the same code into the command line with the variables put in works.

Martin G
  • 25
  • 4
  • Welcome to Stackoverflow. Without example data, this is not solvable. Please add some exemplary data to support your problem. This explains [how to create a good R question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If you edit your post accordingly, I am sure you will find answers. For further guidance please read [how to ask questions](https://stackoverflow.com/help/how-to-ask) and [how to create a minimal example](https://stackoverflow.com/help/mcve) – 5th Sep 14 '18 at 13:34
  • 1
    The main reason probably is, that you are invoking the arrange call with the name of the argument to your function ("outcome") - you might try the tricks explained here: https://www.rdocumentation.org/packages/rlang/versions/0.2.2/topics/quasiquotation – Wolfgang Arnold Sep 14 '18 at 13:36
  • What do you put in the argument `outcome=`? Is it supposed to be a variable name in `dfout`? If so, you need to dive into the world of `dplyr` programming. Take a look at `??dplyr::programming`. Your solution will probably involve using `enquo` and `!!` – divibisan Sep 14 '18 at 16:08
  • Possible duplicate of [dplyr: Standard evaluation and enquo()](https://stackoverflow.com/questions/51277336/dplyr-standard-evaluation-and-enquo) – divibisan Sep 14 '18 at 16:09

1 Answers1

0

You need to evaluate the outcome parameter inside the function, so R will interpret it as a variable, not as text

arranged <- arrange(dfout, State, eval(parse(text=outcome)), Hospital)

Now

   # > best("TX","Heartattack")
   # [1] CYPRESS FAIRBANKS MEDICAL CENTER
Robert
  • 5,038
  • 1
  • 25
  • 43
  • Thank you so much!! I changed that line and it now works. Im still not sure why R didn't evaluate 'outcome' as a variable inside the function to begin with though. – Martin G Sep 14 '18 at 19:27
  • Because `arrange` interpret it as text not variable – Robert Sep 16 '18 at 14:34