0

I have this code here.

library(plyr)

rankhospital <- function(state, illness, rank) {
    outcome <- read.csv("data3/outcome.csv")

    hA <- cbind(as.numeric(as.character(outcome[,11][outcome$State == state])), 
                as.character(outcome$Hospital.Name[outcome$State == state]))
    hF <- cbind(as.numeric(as.character(outcome[,17][outcome$State == state])), 
                as.character(outcome$Hospital.Name[outcome$State == state]))
    pN <- cbind(as.numeric(as.character(outcome[,23][outcome$State == state])), 
                as.character(outcome$Hospital.Name[outcome$State == state]))

    condition <- ifelse(illness == "heart attack", list(hA), ifelse(illness == "heart   failure", list(hF), 
                                                    ifelse(illness == "pneumonia", list(pN), "not a valid input")))

    outcomeData <- as.data.frame(condition)

    rates <- outcomeData[,1]
    hospitals <- outcomeData[,2]

    outcomeData$rank <- ddply(outcomeData,.(hospitals),transform,Order =rank(rates,ties.method = "first")) 

    print(outcomeData$rank)
}

I can use the rank function , however I want to rank alphabetically by hospital first. I can't do this with the rank function. Every time I use a package to rank by an additional variable I get this error.

Error in head(outcomeData) : object 'outcomeData' not found

I've googled this issue and it seems that I have a problem related to the lexical scoping rules of R, however, I'm confused because i've used packages outside of functions fine, I don't understand why I'm getting this issue here.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Keon
  • 1,741
  • 3
  • 17
  • 27
  • a) Your example isn't reproducible, as we don't have `data3/outcome.csv`. Maybe you could share `dput(head(outcome))`? Or see [other tips for reproducibility](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). b) If `outcomeData` isn't found, your problem is probably with the line where it is defined, not several lines later where you try to order it? Maybe? Hard to know without seeing how you're trying to do that. – Gregor Thomas May 30 '14 at 16:15
  • c) There's probably other ways to do it, but I would use `plyr::arrange` or `dplyr::arrange` to order the entire data frame, and then just add a column that's `1:nrow(outcomeData)`. d) You say this is inside a function, and you show the last little `}`, but you don't show the start of the function. – Gregor Thomas May 30 '14 at 16:16
  • Are you trying to do `head(outcomeData)` outside of the function? Because that variable only lives inside the function. But since we are unable to run this code without data, it's hard to tell what's going on. If you really want help, you need to make this example reproducible. – MrFlick May 30 '14 at 20:52

0 Answers0