Posted by: SuperDad July 20, 2015
R-programming. Need help -Code Troubleshooting.
Login in to Rate this Post:     0       ?        
Assignment provides a csv file of "hospital data" consisting of information from all the hospitals across the US, with different variables like Hospital name, State, mortality due to heart attack, heart failure, pneumonia.
File link: https://class.coursera.org/rprog-030/assignment/view?assignment_id=7
So the assignment asks us to write 3 different functions.

So the first question goes like this:

1. 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 file and returns a character vector with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specified 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.

Hint: The function should use the following template.
best <- function(state, outcome) {
## Read outcome data
## Check that state and outcome are valid
## Return hospital name in that state with lowest 30-day death
## rate
}

eg:
> best("TX", "heart attack")
[1] "CYPRESS FAIRBANKS MEDICAL CENTER"
> best("TX", "heart failure")
[1] "FORT DUNCAN MEDICAL CENTER"

My solution to this: (SOLUTION 1)

best <-function (state, outcome) {
data <- read.csv("ProgAssignment3-data/outcome-of-care-measures.csv", colClasses="character")
subset_data <- subset(data, data$State=="state")
if ("outcome" == "heart attack") {
subset_data[,11] <-as.numeric(subset_data[,11])
dat <- subset(subset_data, subset_data[,11]== min(subset_data[,11], na.rm=TRUE))
}
if ("outcome" == "heart failure") {
subset_data[,17] <-as.numeric(subset_data[,17])
dat <- subset(subset_data, subset_data[,17]== min(subset_data[,17], na.rm=TRUE))
}
if ("outcome" == "pneumonia") {
subset_data[,23] <-as.numeric(subset_data[,23])
dat <- subset(subset_data, subset_data[,23]== min(subset_data[,23], na.rm=TRUE))
}
return(dat$Hospital.Name)
}


For this part, I get the error message:
"Error in best("TX", "heart failure") : object 'dat' not found"
However running individual command step by step, without the use of "if else" function gives the output as above example.




Question 2.
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 file and returns a character vector with the name of the hospital that has the ranking specified 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.

Hint:
The function should use the following template.
rankhospital <- function(state, outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
## Return hospital name in that state with the given rank
## 30-day death rate
}

eg.
> rankhospital("TX", "heart failure", 4)
[1] "DETAR HOSPITAL NAVARRO"

[1] "HARFORD MEMORIAL HOSPITAL"
> rankhospital("MN", "heart attack", 5000)
[1] NA


My Solution: SOLUTION2

#Writing a function named "rankhospital"

rankhospital <-function(state, outcome, num) {
data <- read.csv("ProgAssignment3-data/outcome-of-care-measures.csv", colClasses="character")
subset_data <- subset(data, data$State=="state") #acquires subset of data for particular #state where State="state"
if (outcome == "heart attack") {
subset_data[,11] <-as.numeric(subset_data[,11])
dat <- subset_data[order(subset_data[,11],na.last=NA),] #orders/sorts all the column of the #subsetted data in ascending order based on 11th column, removing all the NA values (na.list=NA)
hospital <-dat$Hospital.Name[num]
}
if (outcome == "heart failure") {
subset_data[,17] <-as.numeric(subset_data[,17])
dat <- subset_data[order(subset_data[,17],na.last=NA),]
hospital<-dat$Hospital.Name[num]
}
if (outcome == "pneumonia") {
subset_data[,23] <-as.numeric(subset_data[,23])
dat <- subset_data[order(subset_data[,23],na.last=NA),]
hospital <-dat$Hospital.Name[num]
}
return(hospital)
}



Here in this part, I do not get any warning or error message, however, I get
[1] NA , as an output.


For Question #3, I am still in the process of writing the function.

I know this pieces of informations got longer. But to provide all the details of assignment without loosing the information I thought this was the only way.

Anyone's help will be highly highly appreciated. Thanks in advance.
Read Full Discussion Thread for this article