Skip to content

Instantly share code, notes, and snippets.

@timmyshen
Created October 8, 2013 21:51
Show Gist options
  • Save timmyshen/6892429 to your computer and use it in GitHub Desktop.
Save timmyshen/6892429 to your computer and use it in GitHub Desktop.
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…
best <- function(state, 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')
}
# print(i)
# todo: handle the ties
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(1, 2, i)])
# print(data.state[, i])
# min(data.state[, i]) -> mm
# print(mm)
# print(min(data.state[, i], na.rm=TRUE))
# print(data.state[, i] == min(data.state[, i]))
return.names <- data.state[(data.state[, i] == min(data.state[, i])), ]$Hospital.Name
sort(return.names)[1]
}
@osman4
Copy link

osman4 commented Mar 27, 2015

Hi timmyshen;
I want little assistance from you.I am running your code. when i apply these
best("TX","heart attack")
i get correct information besides i get an error which show NaN values are force to coercion see below error

[1] "CYPRESS FAIRBANKS MEDICAL CENTER"
Warning message:
In best("TX", "heart attack") : NAs introduced by coercion

Kindly guide me who can i modify your code so that NaN values if exited in the data will ignore in the function??

@JessieK
Copy link

JessieK commented May 3, 2015

data <- read.csv(file='outcome-of-care-measures.csv', colClasses = 'character', na.strings = c("NA", "Not Available"))
will take care of NAs

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