# install.packages("data.table") library("data.table") outcome <- data.table::fread('outcome-of-care-measures.csv') outcome[, (11) := lapply(.SD, as.numeric), .SDcols = (11)] outcome[, lapply(.SD , hist , xlab= "Deaths" , main = "Hospital 30-Day Death (Mortality) Rates from Heart Attack" , col="lightblue") , .SDcols = (11)] best <- function(state, outcome) { out_dt <- data.table::fread('outcome-of-care-measures.csv') outcome <- tolower(outcome) chosen_state <- state if (!chosen_state %in% unique(out_dt[["State"]])) { stop('invalid state') } if (!outcome %in% c("heart attack", "heart failure", "pneumonia")) { stop('invalid outcome') } setnames(out_dt , tolower(sapply(colnames(out_dt), gsub, pattern = "^Hospital 30-Day Death \\(Mortality\\) Rates from ", replacement = "" )) ) out_dt <- out_dt[state == chosen_state] col_indices <- grep(paste0("hospital name|state|^",outcome), colnames(out_dt)) out_dt <- out_dt[, .SD ,.SDcols = col_indices] out_dt[, outcome] <- out_dt[, as.numeric(get(outcome))] out_dt <- out_dt[complete.cases(out_dt),] out_dt <- out_dt[order(get(outcome), `hospital name`)] return(out_dt[, "hospital name"][1]) } rankhospital <- function(state, outcome, num) { data <- read.csv("outcome-of-care-measures.csv", colClasses = "character") data <- data[c(2, 7, 11, 17, 23)] names(data)[1] <- "name" names(data)[2] <- "state" names(data)[3] <- "heart attack" names(data)[4] <- "heart failure" names(data)[5] <- "pneumonia" outcomes = c("heart attack", "heart failure", "pneumonia") if( outcome %in% outcomes == FALSE ) { stop("invalid outcome") } states <- data[, 2] states <- unique(states) if( state %in% states == FALSE ) { stop("invalid state") } if( num != "best" && num != "worst" && num%%1 != 0 ) { stop("invalid num") } data <- data[data$state==state & data[outcome] != 'Not Available', ] data[outcome] <- as.data.frame(sapply(data[outcome], as.numeric)) data <- data[order(data$name, decreasing = FALSE), ] data <- data[order(data[outcome], decreasing = FALSE), ] vals <- data[, outcome] if( num == "best" ) { rowNum <- which.min(vals) } else if( num == "worst" ) { rowNum <- which.max(vals) } else { rowNum <- num } data[rowNum, ]$name } rankall <- function(outcome, num = "best") { data <- read.csv("outcome-of-care-measures.csv", colClasses = "character") data <- data[c(2, 7, 11, 17, 23)] names(data)[1] <- "name" names(data)[2] <- "state" names(data)[3] <- "heart attack" names(data)[4] <- "heart failure" names(data)[5] <- "pneumonia" outcomes = c("heart attack", "heart failure", "pneumonia") if( outcome %in% outcomes == FALSE ) stop("invalid outcome") if( num != "best" && num != "worst" && num%%1 != 0 ) stop("invalid num") data <- data[data[outcome] != 'Not Available', ] data[outcome] <- as.data.frame(sapply(data[outcome], as.numeric)) data <- data[order(data$name, decreasing = FALSE), ] data <- data[order(data[outcome], decreasing = FALSE), ] getHospByRank <- function(df, s, n) { df <- df[df$state==s, ] vals <- df[, outcome] if( n == "best" ) { rowNum <- which.min(vals) } else if( n == "worst" ) { rowNum <- which.max(vals) } else { rowNum <- n } df[rowNum, ]$name } states <- data[, 2] states <- unique(states) newdata <- data.frame("hospital"=character(), "state"=character()) for(st in states) { hosp <- getHospByRank(data, st, num) newdata <- rbind(newdata, data.frame(hospital=hosp, state=st)) } newdata <- newdata[order(newdata['state'], decreasing = FALSE), ] newdata }