-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest.R
65 lines (56 loc) · 2.26 KB
/
best.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## The function reads the Outcome-of-care-measures dataset 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 name
## provided in the Hospital.Name variable. The outcomes can be one of "heart attack",
## "heart failure", or "pneumonia". Hospitals that do not have data on a particular
## outcome are be excluded from the set of hospitals when deciding the rankings.
## Ties are broked lexicographically
best <- function(state, outcome) {
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
## Check that the state argument is valid
states <- unique(data$State)
if (!state %in% states) {
stop("invalid state")
}
## Check that the outcome argument is valid
validOutcomes <- c("heart attack", "heart failure", "pneumonia");
if (!outcome %in% validOutcomes) {
stop("invalid outcome")
}
## Find hospital name in that state with lowest 30-day death rate
## Columns
hospitalCol <- 2
stateCol <- 7
deathReasonPctCol <- 23 # Pneumonia column by default
if (outcome=="heart attack") {
deathReasonPctCol <- 11
}
else if (outcome=="heart failure") {
deathReasonPctCol <- 17
}
## Shrink data to the observed state
stateData <- data[data$State==state, c(hospitalCol, deathReasonPctCol)]
## Parsing death reason percentage column to integer
suppressWarnings(stateData [, 2] <- as.numeric(stateData [, 2]))
stateDataOmitted <- stateData[!is.na(stateData[2]), c(1, 2)]
minVal <- min(stateDataOmitted [, 2])
## if there are other mins, break the ties lexicographically
bestHospitals <- stateDataOmitted[stateDataOmitted[, 2]==minVal, c(1, 2)]
best <- min(bestHospitals[, 1])
best
}
## Test cases:
## + best("TX", "heart attack")
## ===>[1] "CYPRESS FAIRBANKS MEDICAL CENTER"
## + best("TX", "heart failure")
## ===>[1] "FORT DUNCAN MEDICAL CENTER"
## + best("MD", "heart attack")
## ===>[1] "JOHNS HOPKINS HOSPITAL, THE"
## + best("MD", "pneumonia")
## ===>[1] "GREATER BALTIMORE MEDICAL CENTER"
## + best("BB", "heart attack")
## ===> invalid state
## + best("NY", "hert attack")
## ===> invalid outcome
## + All passed