Created
October 8, 2013 21:51
-
-
Save timmyshen/6892429 to your computer and use it in GitHub Desktop.
Write a function called best that take two arguments: the 2-character abbreviated name of a state and an
outcome name. The function reads the outcome-of-care-measures.csv le and returns a character vector
with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specied outcome
in that state. The hospital name is the…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
best <- function(state, outcome) { | |
data <- read.csv(file='outcome-of-care-measures.csv', colClasses = 'character') | |
if(!any(state == data$State)) { | |
stop('invalid state') | |
} | |
if(outcome == 'heart attack') { | |
i <- 11 | |
} | |
else if(outcome == 'heart failure') { | |
i <- 17 | |
} | |
else if(outcome == 'pneumonia') { | |
i <- 23 | |
} | |
else { | |
stop('invalid outcome') | |
} | |
# print(i) | |
# todo: handle the ties | |
data.state <- data[data$State == state, ] | |
data.state[, i] <- as.numeric(x=data.state[, i]) | |
data.state <- data.state[complete.cases(data.state), ] | |
# print(data.state[, c(1, 2, i)]) | |
# print(data.state[, i]) | |
# min(data.state[, i]) -> mm | |
# print(mm) | |
# print(min(data.state[, i], na.rm=TRUE)) | |
# print(data.state[, i] == min(data.state[, i])) | |
return.names <- data.state[(data.state[, i] == min(data.state[, i])), ]$Hospital.Name | |
sort(return.names)[1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
data <- read.csv(file='outcome-of-care-measures.csv', colClasses = 'character', na.strings = c("NA", "Not Available"))
will take care of NAs