Skip to content

Instantly share code, notes, and snippets.

@Luckyeva
Last active February 13, 2016 16:13
Show Gist options
  • Save Luckyeva/142bb8fbed18f745cc6d to your computer and use it in GitHub Desktop.
Save Luckyeva/142bb8fbed18f745cc6d to your computer and use it in GitHub Desktop.
R programming Assignment 3 - Part II
## R Programming Assignment 3 - Part II
## Introduction
# The data for this assignment come from the Hospital Compare web site (http://hospitalcompare.hhs.gov)
# run by the U.S. Department of Health and Human Services. The purpose of the web site is to provide data and
# information about the quality of care at over 4,000 Medicare-certified hospitals in the U.S. This dataset essentially
# covers all major U.S. hospitals. This dataset is used for a variety of purposes, including determining
# whether hospitals should be fined for not providing high quality care to patients (see http://goo.gl/jAXFX
# for some background on this particular topic).
# The Hospital Compare web site contains a lot of data and we will only look at a small subset for this
# assignment. The zip file for this assignment contains three files
# - outcome-of-care-measures.csv: Contains information about 30-day mortality and readmission rates for heart attacks,
# heart failure, and pneumonia for over 4,000 hospitals.
# - hospital-data.csv: Contains information about each hospital.
# - Hospital_Revised_Flatfiles.pdf: Descriptions of the variables in each file (i.e the code book).
## TASKS:
## (1) Plot the 30-day mortality rates for heart attack
## (2) Finding the best hospital in a state
## -> (3) Ranking hospitals by outcome in a state
## (4) Ranking hospitals in all states
## (3) Ranking hospitals by outcome in a state
rankhospital <- function (state, outcome_name, num = "best") {
setwd("/Users/lucky1eva/Downloads/rprog-data-ProgAssignment3-data/")
outcome <- read.csv("outcome-of-care-measures.csv")
# Check validity
if (!state %in% State) {
stop("invalid state")
} else if (!outcome_name %in% c("heart attack", "heart failure", "pneumonia")) {
stop("invalid outcome")
} else if (! (num == "worst" | class(num) == "numeric" )) stop("invalid input")
# sort table
State_outcome <- subset(outcome, State == state, select = c("Hospital.Name","State", "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack",
"Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure",
"Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"))
colnames(State_outcome) <- c("Name","State", "heart attack", "heart failure", "pneumonia")
State_outcome[, 3:5] <- apply(State_outcome[, 3:5], 2, function(x) as.numeric(x))
State_outcome[, 1:2] <- apply(State_outcome[, 1:2], 2, function(x) as.character(x))
outcome_sorted <- State_outcome[order(State_outcome[, outcome_name], State_outcome$Name), ]
if (num %in% 1:nrow(outcome_sorted)) {
list(outcome_sorted[num, 1])
} else if (num == "best") {list(outcome_sorted[1, 1])
} else if (num == "worst") {
list(outcome_sorted[sum(!is.na(outcome_sorted[, outcome_name])) , 1])
} else {return(NA)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment