-
Notifications
You must be signed in to change notification settings - Fork 0
/
needimprovement_hospitals.R
41 lines (30 loc) · 1.17 KB
/
needimprovement_hospitals.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
#Finding hospital needing improvement
need_improvement <- function(state, outcome) {
## Read outcome data, check that state and outcome are valid, return hospital
## name in that state with highest 30-day death rate
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
diseases <- c("heart attack", "heart failure", "pneumonia")
if (!(state %in% unique(data[,7]))) {
stop("Invalid State.")
}
if (is.element(outcome, diseases) == FALSE) {
stop("Invalid Outcome.")
}
data <- data[data$State == state, ]
data[, c(11, 17, 23)] <- sapply(data[, c(11, 17, 23)], as.numeric)
data <- data[order(data[, 2]), ]
if (outcome == "heart attack") {
attack_max <- which.max(data[, 11])
data[attack_max, "Hospital.Name"]
}
else if (outcome == "heart failure") {
failure_max <- which.max(data[, 17])
data[failure_max, "Hospital.Name"]
}
else {
pneumo_max <- which.max(data[, 23])
data[pneumo_max, "Hospital.Name"]
}
}
setwd("~/Desktop/hospital_data")
need_improvement("MD", "heart attack") #"HARFORD MEMORIAL HOSPITAL"