Skip to content

Instantly share code, notes, and snippets.

@oskar-j
Created May 31, 2015 13:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save oskar-j/7bb7ccfcfd26dd160cc8 to your computer and use it in GitHub Desktop.
Get rankings for US hospitals split through all states
rankall <- function(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 outcome is valid
if(!is.element(outcome, dissease)){
stop("invalid outcome")
}
## Choose column index basing on outcome arg
if (identical(outcome,dissease[1])) {
scolumn <- 11
} else if (identical(outcome,dissease[2])) {
scolumn <- 17
} else {
scolumn <- 23
}
colname <- names(dataset)[scolumn]
data <- split(dataset, dataset$State)
## Main part - rank all hospitals
if (num == "best") {
result <- lapply(data, function(x) {
head(x[ order(x[,scolumn], x[,c("Hospital.Name")]), ], n=1)
} )
result <- do.call(rbind, lapply(
result, data.frame, stringsAsFactors=FALSE))
result <- result[, c("Hospital.Name","State")]
colnames(result) <- c("hospital","state")
} else if (num == "worst") {
result <- lapply(data, function(x) {
head(x[ order(-x[,scolumn], x[,c("Hospital.Name")]), ], n=1)
} )
result <- do.call(rbind, lapply(
result, data.frame, stringsAsFactors=FALSE))
result <- result[, c("Hospital.Name","State")]
colnames(result) <- c("hospital","state")
} else {
result <- lapply(data, function(x) {
temp <- x[ order(x[,scolumn], x[,c("Hospital.Name")]), ]
tryCatch({ temp[num,] }, error = function (e) {NA} )
} )
result <- do.call(rbind, lapply(
result, data.frame, stringsAsFactors=FALSE))
result <- result[, c("Hospital.Name","State")]
colnames(result) <- c("hospital","state")
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment