Skip to content

Instantly share code, notes, and snippets.

@timmyshen
Last active July 4, 2017 21:59
Show Gist options
  • Save timmyshen/6903760 to your computer and use it in GitHub Desktop.
Save timmyshen/6903760 to your computer and use it in GitHub Desktop.
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 sp…
rankhospital <- function(state, outcome, num = "best") {
# read outcome
data <- read.csv(file="outcome-of-care-measures.csv", colClasses = 'character')
if(!any(state == data$State)) {
stop('invalid state')
}
if(outcome == 'heart attack') {
i <- 11
}
else if(outcome == 'heart failure') {
i <- 17
}
else if(outcome == 'pneumonia') {
i <- 23
}
else {
stop('invalid outcome')
}
data.state <- data[data$State == state, ]
data.state[, i] <- as.numeric(x=data.state[, i])
data.state <- data.state[complete.cases(data.state), ]
# print(data.state[, c(2, i)])
if(num == "best") {
num = 1
}
else if(num == "worst") {
num = nrow(data.state)
}
else if(is.numeric(x=num)) {
# print(num)
if(num<1 || num > nrow(data.state)) {
return(NA)
}
}
else {
stop('invalid num')
}
# print(sort(data.state[, i]))
data.state <- data.state[order(data.state[,i], data.state$Hospital.Name), ]
return.names <- data.state[num, ]$Hospital.Name
# print(return.names)
return.names[1]
}
@prabhat680818
Copy link

The program is not running. It gives error of NA for any input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment