OUTCOME_COLUMNS list heart attack 13 heart failure 19 pneumonia 25 bes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
OUTCOME_COLUMNS = list(
'heart attack' = 13,
'heart failure' = 19,
'pneumonia' = 25
)
best <- function(state, outcome) {
data <- read.csv('outcome-of-care-measures.csv', colClasses='character')
if (! outcome %in% names(OUTCOME_COLUMNS)) {
stop('invalid outcome');
}
rows = data[data$State == state,]
if (nrow(rows) == 0) {
stop('invalid state')
}
#rows[,OUTCOME_COLUMNS[[outcome]]]
outcome_col = rows[,OUTCOME_COLUMNS[[outcome]]]
outcome_col <- as.numeric(outcome_col)
sorted_idx <- order(outcome_col)
sorted_rows <- rows[sorted_idx,]
return(sorted_rows$Hospital.Name[1])
}