-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathbest.R
23 lines (17 loc) · 959 Bytes
/
best.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## best.R
best <- function(state, outcome) {
## Read outcome data
data <- read.csv("./ProgAssignment3-data/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,]
idx <- which.min(as.double(data.state[,colName]))
data.state[idx,"Hospital.Name"]
}