1 Plot the 30-day mortality rates for heart attack Read the outcome data into R via the read.csv function and look at the rst few rows.
outcome <- read.csv("outcome-of-care-measures.csv", colClasses = "character") head(outcome)
There are many columns in this dataset. You can see how many by typing ncol(outcome) (you can see the number of rows with the nrow function). In addition, you can see the names of each column by typing names(outcome) (the names are also in the PDF document.
To make a simple histogram of the 30-day death rates from heart attack (column 11 in the outcome dataset), run
outcome[, 11] <- as.numeric(outcome[, 11])
hist(outcome[, 11])
2 Finding the best hospital in a state Write a function called best that take two arguments: the 2-character abbreviated name of a state and an outcome name. The function reads the outcome-of-care-measures.csv le and returns a character vector with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specied outcome in that state. The hospital name is the name provided in the Hospital.Name variable. The outcomes can be one of \heart attack", \heart failure", or \pneumonia". Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings. Handling ties. If there is a tie for the best hospital for a given outcome, then the hospital names should be sorted in alphabetical order and the rst hospital in that set should be chosen (i.e. if hospitals \b", \c", and \f" are tied for best, then hospital \b" should be returned). The function should use the following template. best <- function(state, outcome) {
} The function should check the validity of its arguments. If an invalid state value is passed to best, the function should throw an error via the stop function with the exact message \invalid state". If an invalid outcome value is passed to best, the function should throw an error via the stop function with the exact message \invalid outcome".
Save your code for this function to a le named best.R.
3 Ranking hospitals by outcome in a state Write a function called rankhospital that takes three arguments: the 2-character abbreviated name of a state (state), an outcome (outcome), and the ranking of a hospital in that state for that outcome (num). The function reads the outcome-of-care-measures.csv le and returns a character vector with the name of the hospital that has the ranking specied by the num argument. For example, the call rankhospital("MD", "heart failure", 5) would return a character vector containing the name of the hospital with the 5th lowest 30-day death rate for heart failure. The num argument can take values \best", \worst", or an integer indicating the ranking (smaller numbers are better). If the number given by num is larger than the number of hospitals in that state, then the function should return NA. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings. Handling ties. It may occur that multiple hospitals have the same 30-day mortality rate for a given cause of death. In those cases ties should be broken by using the hospital name.
The function should use the following template. rankhospital <- function(state, outcome, num = "best") {
} The function should check the validity of its arguments. If an invalid state value is passed to rankhospital, the function should throw an error via the stop function with the exact message \invalid state". If an invalid outcome value is passed to rankhospital, the function should throw an error via the stop function with the exact message \invalid outcome".
Here is some sample output from the function.
source("rankhospital.R") rankhospital("TX", "heart failure", 4) [1] "DETAR HOSPITAL NAVARRO" rankhospital("MD", "heart attack", "worst") [1] "HARFORD MEMORIAL HOSPITAL" rankhospital("MN", "heart attack", 5000) [1] NA Save your code for this function to a file named rankhospital.R.
4 Ranking hospitals in all states Write a function called rankall that takes two arguments: an outcome name (outcome) and a hospital rank- ing (num). The function reads the outcome-of-care-measures.csv le and returns a 2-column data frame containing the hospital in each state that has the ranking specied in num. For example the function call rankall("heart attack", "best") would return a data frame containing the names of the hospitals that are the best in their respective states for 30-day heart attack death rates. The function should return a value for every state (some may be NA). The rst column in the data frame is named hospital, which contains the hospital name, and the second column is named state, which contains the 2-character abbreviation for the state name. Hospitals that do not have data on a particular outcome should be excluded from the set of hospitals when deciding the rankings. Handling ties. The rankall function should handle ties in the 30-day mortality rates in the same way that the rankhospital function handles ties. The function should use the following template.
rankall <- function(outcome, num = "best") {
}
NOTE: For the purpose of this part of the assignment (and for e�ciency), your function should NOT call the rankhospital function from the previous section.
The function should check the validity of its arguments. If an invalid outcome value is passed to rankall, the function should throw an error via the stop function with the exact message \invalid outcome". The num variable can take values \best", \worst", or an integer indicating the ranking (smaller numbers are better). If the number given by num is larger than the number of hospitals in that state, then the function should return NA. Here is some sample output from the function.
source("rankall.R") head(rankall("heart attack", 20), 10)
hospital state AK AK AL D W MCMILLAN MEMORIAL HOSPITAL AL AR ARKANSAS METHODIST MEDICAL CENTER AR
tail(rankall("pneumonia", "worst"), 3) hospital state WI MAYO CLINIC HEALTH SYSTEM - NORTHLAND, INC WI WV PLATEAU MEDICAL CENTER WV WY NORTH BIG HORN HOSPITAL DISTRICT WY
Save your code for this function to a file named rankall.R. 5