-1

I want to return 2-column data frame containing the hospital in each state that has the specified ranking.

here is an inputs:

rankall <- function(outcome, num = "best") {
data_frame <- data.frame()
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
outcomes <- c("heart attack", "heart failure", "pneumonia")
if(!outcome %in% outcomes){stop("invalid outcome")}
df <- subset(data, select = c(Hospital.Name,State,Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack,
                            Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure,
                            Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia))

Here is a part of my code:

for (i in states){
    if(outcome == "heart attack"){
        if(num =="best"){
            m <- df$Hospital.Name[which.min(df[ ,3])]
            data_frame <- rbind(data_frame,data.frame(m,i))
            }else if(num == "worst"){
                m <- df$Hospital.Name[which.max(df[ ,3])]
                data_frame <- rbind(data_frame,data.frame(m,i))
                }else{
                    df <- df[order(df[,3],df[["Hospital.Name"]],decreasing=FALSE,na.last=NA),]
                    m<- df[num,"Hospital.Name"]
                    data_frame <- rbind(data_frame,data.frame(m,i))

The problem is, that my data.frame of hospitals returns me one hospital for all states: here is my code:

 head(rankall("heart attack", 20), 10)
m  i
1  NORTHWESTERN MEMORIAL HOSPITAL AL
2  NORTHWESTERN MEMORIAL HOSPITAL AK
3  NORTHWESTERN MEMORIAL HOSPITAL AZ
4  NORTHWESTERN MEMORIAL HOSPITAL AR
5  NORTHWESTERN MEMORIAL HOSPITAL CA
6  NORTHWESTERN MEMORIAL HOSPITAL CO
7  NORTHWESTERN MEMORIAL HOSPITAL CT
8  NORTHWESTERN MEMORIAL HOSPITAL DE
9  NORTHWESTERN MEMORIAL HOSPITAL DC
10 NORTHWESTERN MEMORIAL HOSPITAL FL

Can you please advice me how to return data frame containing the hospitals for each state with specified ranking?

data

structure(list(Hospital.Name = c("SOUTHEAST ALABAMA MEDICAL CENTER", 
"MARSHALL MEDICAL CENTER SOUTH", "ELIZA COFFEE MEMORIAL HOSPITAL", 
"MIZELL MEMORIAL HOSPITAL", "CRENSHAW COMMUNITY HOSPITAL", "MARSHALL MEDICAL CENTER NORTH", 
"ST VINCENT'S EAST", "DEKALB REGIONAL MEDICAL CENTER", "SHELBY BAPTIST MEDICAL CENTER", 
"CALLAHAN EYE FOUNDATION HOSPITAL", "HELEN KELLER MEMORIAL HOSPITAL", 
"DALE MEDICAL CENTER", "CHEROKEE MEDICAL CENTER", "BAPTIST MEDICAL CENTER SOUTH", 
"JACKSON HOSPITAL & CLINIC INC"), State = c("AL", "AL", "AL", 
"AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL", 
"AL"), Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack = c("14.3", 
"18.5", "18.1", "Not Available", "Not Available", "Not Available", 
"17.7", "18.0", "15.9", "Not Available", "19.6", "17.3", "Not Available", 
"17.8", "17.5"), Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure = c("11.4", 
"15.2", "11.3", "13.6", "13.8", "12.5", "10.9", "16.6", "13.6", 
"Not Available", "12.6", "11.8", "12.1", "11.8", "10.2"), Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia = c("10.9", 
"13.9", "13.4", "14.9", "15.8", "8.7", "16.2", "15.8", "10.7", 
"Not Available", "15.0", "9.9", "14.7", "14.3", "14.7")), .Names = c("Hospital.Name", 
"State", "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack", 
"Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure", 
"Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"), row.names = c(NA, 
15L), class = "data.frame")
Pierre L
  • 28,203
  • 6
  • 47
  • 69
Daniel Yefimov
  • 860
  • 1
  • 10
  • 24
  • 1
    Can you provide few lines of input dataset and expected output based on that – akrun Aug 10 '15 at 12:37
  • 2
    Start by reading [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [this](http://stackoverflow.com/help/how-to-ask) – David Arenburg Aug 10 '15 at 12:39
  • 2
    Your example is still not reproducible. Please provide some of your data by posting the result of `dput(head(yourdata,30))` and an example of your desired output. – Heroka Aug 10 '15 at 12:52
  • 4
    I'm voting to close this until you provide a reproducible example as per all the guidance you've received until now. – David Arenburg Aug 10 '15 at 13:37
  • 2
    You may want to read the [open letter to students with homework problems](http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems). – Jaap Aug 10 '15 at 13:45
  • I added some sample data from the first part of your code. But to diagnose the problem further, you should supply how you defined `states` in the line `for (i in states){` – Pierre L Aug 10 '15 at 13:53
  • This looks a lot like a coursera problem... Irony being due to the way the course is structured there's an even simpler way to cheat. – DaveRGP Aug 10 '15 at 14:31

1 Answers1

0
rankall <- function(outcome, num = "best") {
  data_frame <- data.frame()
  data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
  outcomes <- c("heart attack", "heart failure", "pneumonia")
  if(!outcome %in% outcomes){stop("invalid outcome")}
  df <- subset(data, select = c(Hospital.Name,State,Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack,
                            Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure,
                            Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia))

  df[,3:5] <- suppressWarnings(lapply(df[,3:5], as.numeric))
  states <- unique(df$State)  
  for (i in states){
    if(outcome == "heart attack"){
        if(num =="best"){
            m <- df$Hospital.Name[which.min(df[,3][df$State == i])]
            data_frame <- rbind(data_frame,data.frame(m,i))
            }else if(num == "worst"){
                m <- df$Hospital.Name[which.max(df[,3][df$State == i])]
                data_frame <- rbind(data_frame,data.frame(m,i))
                }else{
                    df1 <- df[order(df[,3][df$State == i],df$Hospital.Name[df$State == i],decreasing=FALSE,na.last=NA),]
                    m<- df1[num,"Hospital.Name"]
                    data_frame <- rbind(data_frame,data.frame(m,i))}
    }
  }
  data_frame[order(data_frame$i),]
}

I made a few changes to the code:

  1. Coerced the measurement columns to class numeric. With the line df[,3:5] <- suppressWarnings(lapply(df[,3:5], as.numeric)) When you call the functions which.min and which.max, numeric columns are needed.

  2. Added the subset for the state in question with the line df[,3][df$State == i] The reason you were getting the same hospital for each state, is because you were not including the index in your subset.

  3. Added the output line data_frame[order(data_frame$i),] at the end to return the ending data frame in alphabetical order by hospital.

Pierre L
  • 28,203
  • 6
  • 47
  • 69