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