【问题标题】:I wrote the function, but it works inconsistently, R, Coursera, Best Hospital assignment我编写了函数,但它的工作方式不一致,R,Coursera,Best Hospital assignment
【发布时间】:2016-05-12 04:44:42
【问题描述】:

我正在学习 R,并试图解决 Coursera 最佳医院的任务。出于某种原因,我的函数的结果不一致。它仅在 6 个样本输出中的 4 个中给出正确答案。我浏览了互联网,已经摆脱了一路上的所有警告。但无法弄清楚功能错误。感谢您的时间以及您可以就如何改进功能提供的任何建设性的 cmets。非常感谢大家。

    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

这里应该打印“约翰霍普金斯医院”

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

这里还有“大巴尔的摩医疗中心”

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

正确

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

正确

【问题讨论】:

  • 不清楚你的函数试图做什么。我建议您使用实际目标、输入数据示例以及您期望的输出来编辑您的问题。阅读how to make a great reproducible example。这会让其他人更有可能帮助你。

标签: r function


【解决方案1】:

错误就在这里:

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

【讨论】:

    【解决方案2】:

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

      # 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

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2020-04-20
      相关资源
      最近更新 更多