[使用命令函数后用什么语法选择行?

问题描述 投票:0回答:1

我有获得三个参数的代码。它被州缩小。然后根据条件缩小范围。最后,根据其在以下情况(心脏病发作,心力衰竭或肺炎)下的表现,按医院级别对其进行缩小。对于此代码,我正在处理代码的心力衰竭部分,因此可以忽略其他两个代码。排序功能很好地排序了心力衰竭率。但是,此后我很难选择排名。

best("AK","heart failure", 3)

best <- function(state, outcome, num) {   
  #Reads the csv file
  dataTable  <- read.csv("outcome.csv", header = TRUE, stringsAsFactors = FALSE)

  #Passes the state argument to the choice variable
  choice <- state
  stateOfChoice <- dataTable[dataTable$State == choice,]

  #Makes sure that only three of outcomes found in the csv file are selected
  if(outcome != "heart failure" && outcome != "heart attack" && outcome != "pneumonia"){
     print("wrong condition, try again")
     main()
   }

   #using the selected rows from above, return the minimum value of rate from heart attack and then use this selected row to find the hospital name
   else if (outcome == "heart attack"){
     heart_attack <- stateOfChoice[which.min(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack), ]
     hospital <- heart_attack$Hospital.Name

     return(hospital)
   }

   #Similar as above, but instead with heart failure
   else if (outcome == "heart failure"){
     orderState <- stateOfChoice[order(as.integer(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure), decreasing = FALSE),]
        orderStateNum <- orderState$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure[[num]]

      ##heart_failure <- stateOfChoice[which.min(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure),]
        hospital <- orderStateNum$Hospital.Name
        return(hospital)
    }

    #Similar as above, but instead with pneumonia
    else if (outcome == "pneumonia"){
       pneumonia <- stateOfChoice[which.min(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia),]
       hospital <- pneumonia$Hospital.Name
       return(hospital)
    } 
}

例如,您可以看到由于orderState <- stateOfChoice[order(as.integer(stateOfChoice$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure),decreasing = FALSE),]条件heart failure,order函数在此变量下很好地对行进行了排序。第三个选择应为#100,它与医院名称Mat-su regional medical center相对应。我没有那个医院的名字。我得到#101,与Bartlett地区医院相对应。

   Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure

115                                                       10.8
104                                                       11.2

100                                                       11.4

114                                                       11.4
101                                                       11.6

数据在这里:

Pls click for dataset

r subset
1个回答
0
投票

不确定要理解的内容。此外,您的代码无法正常工作,并且出现以下错误:

© www.soinside.com 2019 - 2024. All rights reserved.