R min函数未返回最小值

时间:2016-09-17 19:17:41

标签: r function min

第一次在这里发帖,真的很感激帮助。 我正在关于Coursera的数据科学专业化进行R编程任务,但我正在努力解决一个问题。

best <- function(state, outcome) {
  directory <- setwd("/Users/SParikh3/Desktop/rprog-data-ProgAssignment3-data")
  data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
  table <- data.frame(cbind(data[,2], data[,7], data[,11], data[,17],   data[,23]))
  colnames(table) <- c("Hospital", "State", "Heart Attack", "Heart Failure", "Pneumonia")

  if(!state %in% table[,"State"]){
    stop("Invalid state")
  }
  else if (!outcome %in% c("Heart Attack", "Heart Failure", "Pneumonia")) {
    stop("Invalid outcome")
  }
  else {
    table <- subset(table, State == state, select = c(outcome, "Hospital"))
    x <- as.numeric(table[,outcome])
    minimum <- min(x, na.rm = TRUE)
    result <- table[, "Hospital"][which(x == minimum)]
    output <- result[order(result)]
  }
  return(output[1])
}

这是我的代码,由于某种原因,最小值没有返回正确的最小值。当我选择心脏病发作而不是其他结果时它会起作用。例如,如果心力衰竭的最小值为5,则返回类似于7的任何原因。为什么会出现这种情况?请告诉我!谢谢!

0 个答案:

没有答案
相关问题