-
Notifications
You must be signed in to change notification settings - Fork 1
/
best.R
36 lines (31 loc) · 1.29 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
best <- function(state, outcome) {
##Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
outcomes <- c("heart attack", "heart failure", "pneumonia")
states <- unique(data$State)
##Check if state is valid
if (!(state %in% states)) {
stop("invalid state")
}
##Check if outcome is valid
if (!(outcome %in% outcomes)) {
stop("invalid outcome")
}
##Finalize search range
search_out <- if (identical(outcome, outcomes[1])) {
"Heart.Attack"
} else if (identical(outcome, outcomes[2])) {
"Heart.Failure"
} else {
"Pneumonia"
}
search_col <- paste("Hospital.30.Day.Death..Mortality..Rates.from",
search_out, sep = ".")
##Find the lowest mortality rate
rates <- as.numeric(data[data$State == state, search_col])
bestRate <- min(rates, na.rm = T)
##Find hospital that corresponds to lowest mortality rate
bestIdx <- which(data$State == state &
data[search_col] == format(bestRate, nsmall = 1))
data[bestIdx, "Hospital.Name"]
}