Skip to content

Instantly share code, notes, and snippets.

@kfeoktistoff
Created June 21, 2014 19:13
Show Gist options
  • Save kfeoktistoff/cc127819122f404156f9 to your computer and use it in GitHub Desktop.
Save kfeoktistoff/cc127819122f404156f9 to your computer and use it in GitHub Desktop.
Programming assignment 3 for Coursera "R Programming" course by Johns Hopkins University
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
source("sortHospitalsByOutcome.R")
head(sortHospitalsByOutcome(state, outcome), 1)
}
outcomeCol <- function(outcome) {
if (outcome == "heart attack") {
outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack"
} else if (outcome == "heart failure") {
outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"
} else if (outcome == "pneumonia") {
outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"
}
else {
stop("invalid outcome")
}
}
rankall <- function(outcome, num="best") {
## Read outcome data
## Check that state and outcome are valid
## For each state, find the hospital of the given rank
## Return a data frame with the hospital names and the
## (abbreviated) state name
source("outcomeCol.R")
outcome <- outcomeCol(outcome)
data <- read.csv("data/outcome-of-care-measures.csv", colClasses="character")
data[,outcome] <- suppressWarnings(as.numeric(data[,outcome]))
data <- data[order(data$"State", data[outcome], data$"Hospital.Name", na.last=NA),]
data <- data[!is.na(outcome)]
l <- split(data[,c("Hospital.Name")], data$State)
rankHospitals <- function(x, num) {
if (num=="best") {
head(x, 1)
} else if (num=="worst") {
tail(x, 1)
} else {
x[num]
}
}
result <- lapply(l, rankHospitals, num)
data.frame(hospital = unlist(result), state = names(result), row.names = names(result))
}
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
source("best.R")
source("sortHospitalsByOutcome.R")
if (num=="best") {
best(state, outcome)
} else if (num=="worst") {
tail(sortHospitalsByOutcome(state, outcome), 1)
} else {
sortHospitalsByOutcome(state, outcome)[num]
}
}
sortHospitalsByOutcome <- function(state, outcome) {
source("outcomeCol.R")
outcome <- outcomeCol(outcome)
data <- read.csv("data/outcome-of-care-measures.csv", stringsAsFactors=FALSE)
if (!state %in% data$State) {
stop("invalid state")
}
data <- data[data$State==state,]
data[,outcome] <- suppressWarnings(as.numeric(data[,outcome]))
data <- data[order(data[outcome], data$"Hospital.Name"),]
as.character(data$"Hospital.Name"[!is.na(data[outcome])])
}
@bbrewington
Copy link

You should take this down, since you're enabling other people to copy it and cheat. Also, this goes against the Coursera Honor Code (link: https://www.coursera.org/about/terms/honorcode)

It says "I will not make solutions to homework, quizzes, exams, projects, and other assignments available to anyone else (except to the extent an assignment explicitly permits sharing solutions). This includes both solutions written by me, as well as any solutions provided by the course staff or others."

@rahul351987
Copy link

Good one but calling functions in other codes makes it difficult plus time consuming!

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