Created
June 21, 2014 19:13
-
-
Save kfeoktistoff/cc127819122f404156f9 to your computer and use it in GitHub Desktop.
Programming assignment 3 for Coursera "R Programming" course by Johns Hopkins University
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])]) | |
} |
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."
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
best <- function(state, outcome ) {
Read outcome data----------------------------------------
ERROR MSG for Outcomes-----------------------------------
ERROR MSG for State--------------------------------------
Create Data Frame with Hosp., State, H. Attack, H. Failure & Pneumonia-----
Replace 'Not Available with NA---------------------------------------------
Find minimum value of the Outcome--------------------------------------------
Display all hospitals with the minimum dead rate-----------------------------
Display result in Alphabetical ordered------------------------------------------
Display first value from the result vector-------------------------------------
}