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])]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good one but calling functions in other codes makes it difficult plus time consuming!