Get rankings for US hospitals split through all states
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
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