-3

I am learning R and was trying to solve the Coursera Best Hospital assignment. For some reason, my function's results are inconsistent. It gives correct answers only in 4 out of 6 sample outputs. I looked through the internet and already got rid of all the warnings I had along the way. But cannot figure out the function bug. I appreciate your time and any constructive comments you can provide on how to improve the function. Thank you all very much.

    best <- function (state, outcome) {

      data <- read.csv("outcome-of-care-measures.csv", header = TRUE, colClasses = "character", 
               na.strings = "Not Available")

      select_o <- if (outcome == "heart attack") {
        "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack"
      } else if (outcome == "heart failure") {
        "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"
      } else if (outcome == "pneumonia") {
        "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"
      } else {
        stop("invalid outcome")
      }

      for (i in nrow(data)) {
        if(data$State[i] != state) {
          stop("invalid state")}
      }
        best_data <- data[data$State == state, c("Hospital.Name", select_o)]
        na.omit(best_data)
        best_data[, select_o] <- as.numeric(best_data[, select_o])
        ordered <- order(best_data[, select_o], best_data[, "Hospital.Name"])

        as.character(best_data[, "Hospital.Name"][ordered[1]])

    }

When I test sample outputs I get these results:

        > best("TX", "heart attack")
        [1] "CYPRESS FAIRBANKS MEDICAL CENTER"

correct

        > best("TX", "heart failure")
        [1] "FORT DUNCAN MEDICAL CENTER"

correct

        > best("MD", "heart attack")
        Error in best("MD", "heart attack") : invalid state

it should print " Johns Hopkins Hospital, The" here

        > best("MD", "pneumonia")
        Error in best("MD", "pneumonia") : invalid state

and "Greater Baltimore Medical Center" here

        > best("BB", "heart attack")
        Error in best("BB", "heart attack") : invalid state

correct

        > best("NY", "hert attack")
        Error in best("NY", "hert attack") : invalid outcome

correct

  • 1
    It's not clear what your function is trying to do. I suggest you edit your question with what the actual goal is, an example of the input data, and what you expect the output to be. Have a read of [how to make a great reproducible example](http://stackoverflow.com/q/5963269/5977215). This will make other people more likely to help you. – SymbolixAU May 12 '16 at 05:10

3 Answers3

2

The error lies here:

for (i in nrow(data)) {

Actually, when you apply nrow() function over data.frame, a one-length vector is returned signifying the number of rows in the data.frame.

So, in the above code, when you use i in nrow(data), i will have only one value that is the number of rows in data.

If you want to iterate over each and every row of the data.frame, change the code to following:

Solution:

valid_state <- F
for (i in 1:nrow(data)) {
    if(data$State[i] == state){ 
        valid_state <- T
        break
    }
}

if (!valid_state)
    stop("invalid state")
Kunal Puri
  • 3,419
  • 1
  • 10
  • 22
1

Thank you, everyone, the issue was fixed in the below code;

  # First, we load "Outcome of Care Measures" data table into R and inspect it:

  outcome <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
  head(outcome)
  ncol(outcome)
  nrow(outcome)
  names(outcome)

  # 1. Let's build a simple plot for the 30-day death rates from heart attack (column 11):

  outcome[, 11] <- as.numeric(outcome[, 11])
  hist(outcome[, 11])

  # 2. Now we will create a function searching for the best hospital in state.

  best <- function (state, outcome) {
  my_data <- read.csv("outcome-of-care-measures.csv", colClasses = "character", na.strings = "Not Available")

  select_o <- if (outcome == "heart attack") {
    "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack"
  } else if (outcome == "heart failure") {
    "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"
  } else if (outcome == "pneumonia") {
    "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"
  } else {
    stop("invalid outcome")
  }

  # Let's check state validity
  valid_state <- which(my_data$State == state)

  # we create a loop, if any of the table states names match given state name, 
  # then mark state as valid; if no states match, mark invalid
  for (i in 1:nrow(my_data)) {
    if(my_data$State[i] == state) {
      valid_state
      break} 
    if (length(valid_state) == 0) {
      stop("invalid state")}
  }

  # now we select only observations that match the state and the outcome specified by user
    best_data <- my_data[valid_state, c("Hospital.Name", select_o)] 

  # make sure selected outcome column is numeric:
    best_data[, select_o] <- as.numeric(best_data[, select_o])

  # order our new table by outcome and Hospital.Name
    ordered <- order(best_data[, select_o], best_data[, "Hospital.Name"])

  # To select the Hosital with the lowest mortality rate, we just need to select 
  # the first element in our ordered table
    as.character(best_data[, "Hospital.Name"][ordered[1]])
  }

  # sample inputs:
  best("TX", "heart attack")
  best("TX", "heart failure")
  best("MD", "heart attack")
  best("MD", "pneumonia")
  best("BB", "heart attack")
  best("NY", "hert attack")

I posted a full code here: https://github.com/brklngirl/ProgrammingAssignment3/blob/master/best.R

Matt
  • 74,352
  • 26
  • 153
  • 180
  • In future, please [include the essential parts of the answer here, and provide the link for reference](//meta.stackoverflow.com/q/8259). – Matt May 14 '16 at 08:53
0

I'm doing same assignment. Have a look at how I solved my problem.

valid_list <- c("heart attack", "heart failure", "pneumonia")
if (any(valid_list == x) ) {

} else {

    stop("invalid measure")
}

care_outcome <-read_care_outcome()
valid_state <-unique(care_outcome[,7])

if (any(valid_state == state) ) {

} else {

    stop("invalid state")
}
clemens
  • 16,716
  • 11
  • 50
  • 65
Noufal E
  • 51
  • 2
  • 6