-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankhospital.R
25 lines (20 loc) · 1.08 KB
/
rankhospital.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
rankhospital <- function(state, outcome,num="best") {
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character",na.strings="Not Available")
## Check that state and outcome are valid
validOutcome = c("heart attack","heart failure","pneumonia")
if (!outcome %in% validOutcome) { stop("invalid outcome")}
validState = unique(data[,7])
if (!state %in% validState) stop("invalid state")
## convert outcome name into column name
fullColName <- c("Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack", "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure", "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia")
colName <- fullColName[match(outcome,validOutcome)]
## Return hospital name in that state with lowest 30-day death rate
data.state <- data[data$State==state,]
order.rate<-order(as.numeric(data.state[,colName]),data.state[,2],na.last = TRUE,decreasing=FALSE)
new.data<-data.state[order.rate,2]
last<-sum(!is.na(data.state[,colName]))
if (num=="best") num=1
if (num=="worst") num=last
new.data[num]
}