-
-
Save oskar-j/a8825a6794bd1a0f3aa9 to your computer and use it in GitHub Desktop.
Get rankings for US hospitals for a given state
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
rankhospital <- function(state, outcome, num = "best") { | |
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), | |
] | |
if (num == "best") { | |
result <- head( data[ order(data[,scolumn], | |
data[,2]), c("Hospital.Name")] , n = 1) | |
result | |
} else if (num == "worst") { | |
result <- tail( data[ order(data[,scolumn], | |
data[,2]), c("Hospital.Name")] , n = 1) | |
result | |
} else { | |
result <- data[ order(data[,scolumn], | |
data[,2]), c("Hospital.Name")] | |
tryCatch({ result[num] }, error = function (e) {NA} ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment