I have written a function in R to rank the hospitals based on the mortality rates for 3 different outcomes(heart attack, heart failure and pneumonia) for each state in the US. The function takes 2 arguments namely the outcome and num(the ranking of a hospital in that state for that outcome). Everything works fine except that I need to include a condition that will return NA if num is greater than the number of hospitals in that state and when I do that it by bypasses all the other if/else if statements. If I don't include that condition, the function works fine. For example,
head(rankall("heart attack", 20), 10)
[1] NA
The required output is:
hospital_name_all state_list
1 <NA> AK
2 D W MCMILLAN MEMORIAL HOSPITAL AL
3 ARKANSAS METHODIST MEDICAL CENTER AR
4 JOHN C LINCOLN DEER VALLEY HOSPITAL AZ
5 SHERMAN OAKS HOSPITAL CA
6 SKY RIDGE MEDICAL CENTER CO
7 MIDSTATE MEDICAL CENTER CT
8 <NA> DC
9 <NA> DE
10 SOUTH FLORIDA BAPTIST HOSPITAL FL
rankall <- function(outcome, num="best"){
outcome.data <- read.csv("outcome-of-care-measures.csv", colClasses="character")
outcome.data[, 11] <- as.numeric(outcome.data[, 11]) # heart attack
outcome.data[, 17] <- as.numeric(outcome.data[, 17]) # heart failure
outcome.data[, 23] <- as.numeric(outcome.data[, 23]) # pneumonia
req_outcome <- c("heart attack","heart failure","pneumonia")
state_list<-sort(unique(outcome.data[,7]))
hospital_name_all<- rep("", length(state_list))
if (!outcome %in% req_outcome) stop("invalid outcome")
for (i in 1:length(state_list)){
state.sub<-outcome.data[outcome.data[,7]==state_list[i],]
limit<-length(state.sub[,2])
if (outcome=="heart attack"){
mortality.val<-state.sub[,c(2,11)]}
else if (outcome=="heart failure"){
mortality.val<-state.sub[,c(2,17)]}
else if (outcome=="pneumonia"){
mortality.val<-state.sub[,c(2,23)]}
if (num=="best"){
rank.hosp<- mortality.val[order(mortality.val[,2],mortality.val[,1]),]
best_rank<-min(rank.hosp[,2],na.rm = TRUE)
min_ind<-which(rank.hosp[,2]==best_rank)
hospital_name_all[i]<-rank.hosp[min_ind,1]
}
else if (num=="worst"){
rank.hosp<- mortality.val[order(mortality.val[,2],mortality.val[,1]),]
worst<-max(rank.hosp[,2],na.rm = TRUE)
max_ind<-which(rank.hosp[,2]==worst)
hospital_name_all[i]<-rank.hosp[max_ind,1]
}
else if (num %in% 1:length(state.sub[,2])){
hospital_name_all[i]<-mortality.val[,1][order(mortality.val[,2],mortality.val[,1])[num]]
return(num)
}
#if (num > limit) {return(NA)}
}
rank_hosp_df<-data.frame(hospital_name_all,state_list)
return(rank_hosp_df)
}
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire