-
-
Save oskar-j/22a42a59191bed2522ee to your computer and use it in GitHub Desktop.
Gets best hospital given arguments - State and type of rating
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) { | |
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