Skip to content

Instantly share code, notes, and snippets.

@oskar-j

oskar-j/best.R Secret

Created May 31, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oskar-j/22a42a59191bed2522ee to your computer and use it in GitHub Desktop.
Save oskar-j/22a42a59191bed2522ee to your computer and use it in GitHub Desktop.
Gets best hospital given arguments - State and type of rating
best <- function(state, outcome) {
dissease <- c("heart attack", "heart failure", "pneumonia")
## Read data
dataset <- read.csv("outcome-of-care-measures.csv",
colClasses = "character")
dataset[, 11] <- suppressWarnings ( as.numeric(dataset[, 11]) )
dataset[, 17] <- suppressWarnings ( as.numeric(dataset[, 17]) )
dataset[, 23] <- suppressWarnings ( as.numeric(dataset[, 23]) )
## Check that state and outcome are valid
if(!is.element(outcome, dissease)){
stop("invalid outcome")
}
if(!state %in% unique(dataset[!is.na(dataset$State), c("State")]) ){
stop("invalid state")
}
## Return hospital name in that state
## with lowest 30-day death rate
if (identical(outcome,dissease[1])) {
scolumn <- 11
}
else if (identical(outcome,dissease[2])) {
scolumn <- 17
}
else {
scolumn <- 23
}
data <- dataset[
which(!is.na(dataset[, scolumn])
& dataset$State %in% state),
]
head( data[ order(data[,scolumn]), c("Hospital.Name")] , n = 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment