我写了这个函数,但它的工作不一致,R,Coursera,最佳医院任务

时间:2016-05-12 04:44:42

标签: r function

我正在学习R并且正在努力解决Coursera最佳医院的任务。出于某种原因,我的功能结果不一致。它仅在6个样本输出中的4个中给出正确答案。我浏览了互联网,已经摆脱了我沿途的所有警告。但无法弄清楚功能错误。我感谢您的时间和任何有关如何改进功能的建设性意见。非常感谢你们。

    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]])

    }

当我测试样本输出时,我得到了这些结果:

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

正确

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

正确

        > best("MD", "heart attack")
        Error in best("MD", "heart attack") : invalid state
它应该打印&#34;约翰霍普金斯医院,&#34;这里

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

和&#34;大巴尔的摩医疗中心&#34;这里

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

正确

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

正确

3 个答案:

答案 0 :(得分:2)

错误在于:

for (i in nrow(data)) {

实际上,当你在data.frame上应用nrow()函数时,会返回一个长度向量,表示data.frame中的行数。

因此,在上面的代码中,当您使用i in nrow(data)时,i将只有一个值,即data中的行数。

如果要迭代data.frame的每一行,请将代码更改为以下内容:

解决方案:

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

if (!valid_state)
    stop("invalid state")

答案 1 :(得分:1)

谢谢大家,问题已在以下代码中修复;

  # 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")

我在这里发布了完整的代码:https://github.com/brklngirl/ProgrammingAssignment3/blob/master/best.R

答案 2 :(得分:0)

我做同样的任务。看看我是如何解决我的问题的。

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")
}