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