week 4 assignment hospital quality

best<-function(state,outcome){
    ##read outcome data
    data<-read.csv("outcome-of-care-measures.csv")[c(2,7,11,17,23)]
    ##check that state and outcome are valid
    if ( !state %in% data$State ){
          stop("invalid state")
    }else if(!outcome %in% c("heart attack","heart failure","pneumonia")){
          stop("invalid outcome")
    }else{
          ## return hospital name in that state with lowest 30-day death rate
          ##筛选出制定的州的数据
          data1<-split(data,data$State)[[state]]
          var<-names(data)
          re_outcome<-var[3:5]
          new_outcome<-strsplit(outcome," ")[[1]]
          real_outcome<-grep(new_outcome[length(new_outcome)],re_outcome,ignore.case = TRUE,value = TRUE)
          ##考虑存在多个最小值的情况
          ##row_num<-which(as.numeric(data1[[real_outcome]])==min(as.numeric(data1[[real_outcome]]),na.rm=TRUE),arr.ind=TRUE)
          ##选择字母表顺序在第一位的医院
          rank<-arrange(data1,as.numeric(data1[[real_outcome]]),data1$Hospital.Name)
          rank$Hospital.Name[1]
          ##sort(data1$Hospital.Name[row_num])[1]
    }
}
rankhospital <- function(state, outcome, num = "best") {
  ## Read outcome data
  data<-read.csv("outcome-of-care-measures.csv")[c(2,7,11,17,23)]
  ## Check that state and outcome are valid
  if ( !state %in% data$State ){
    stop("invalid state")
  }else if(!outcome %in% c("heart attack","heart failure","pneumonia")){
    stop("invalid outcome")
  }else{
    data1<-split(data,data$State)[[state]]
    var<-names(data1)
    re_outcome<-var[c(3:5)]
    new_outcome<-strsplit(outcome," ")[[1]]
    real_outcome<-grep(new_outcome[length(new_outcome)],re_outcome,ignore.case = TRUE,value = TRUE)
    ##得到一个ranking,并剔除outcome列中含有NA的行
    rank1<-arrange(data1,as.numeric(data1[[real_outcome]]),data1$Hospital.Name)
    rank2<-rank1[complete.cases(as.numeric(rank1[[real_outcome]])),]
    ##判断输入的num是否超过rank长度
    if(num=="best"){
        rank2$Hospital.Name[1]
    }else if(num=="worst"){
        rank2$Hospital.Name[nrow(rank2)]
    }else{
        rank2$Hospital.Name[num]
    }
  ## Return hospital name in that state with the given rank
  ## 30-day death rate
  }
  
}
rankall <- function(outcome, num = "best") {
  ## Read outcome data
  data<-read.csv("outcome-of-care-measures.csv")[c(2,7,11,17,23)]
  ## Check that outcome is valid
  if (!outcome %in% c("heart attack","heart failure","pneumonia")){
      stop("invalid outcome")
  }else{
  ## For each state, find the hospital of the given rank
    var<-names(data)
    data1<-split(data,data$State)
    re_outcome<-var[3:5]
    new_outcome<-strsplit(outcome," ")[[1]]
    real_outcome<-grep(new_outcome[length(new_outcome)],re_outcome,ignore.case = TRUE,value = TRUE)
    rank_1<-lapply(data1,function(x,y=num){
        rank1<-arrange(x,as.numeric(x[[real_outcome]]),x$Hospital.Name)
        rank2<-rank1[complete.cases(as.numeric(rank1[[real_outcome]])),]
        ##判断输入的num是否超过rank长度
        if(y=="best"){
          c(rank2$Hospital.Name[1],rank2$State[1])
        }else if(y=="worst"){
          c(rank2$Hospital.Name[nrow(rank2)],rank2$State[1])
        }else{
          c(rank2$Hospital.Name[y],rank2$State[1])
        }
      
    })
  ## Return a data frame with the hospital names and the
  ## (abbreviated) state name
    data.frame(matrix(lapply(rank_1,as.character)))
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值