-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankhospital.R
51 lines (42 loc) · 1.62 KB
/
rankhospital.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
# TODO: Add comment
#
# Author: Anna Matysik
###############################################################################
rankhospital <- function(state, outcome, num = "best") {
## Check that outcome is valid
outcome.column.no <- NULL
if (outcome == "heart attack") {
outcome.column.no <- 11
} else if (outcome == "heart failure") {
outcome.column.no <- 17
} else if (outcome == "pneumonia") {
outcome.column.no <- 23
} else {
stop("invalid outcome")
}
## Read outcome data
outcome.care.measures <- read.csv("outcome-of-care-measures.csv", colClasses = "character", header=TRUE)
outcome.care.measures[ ,outcome.column.no] <- as.numeric(outcome.care.measures[ ,outcome.column.no])
outcome.care.measures <- outcome.care.measures[!is.na(outcome.care.measures[,outcome.column.no]), ]
## Check that state is valid
state <- toupper(state)
if (state %in% outcome.care.measures[ ,"State"]) {
## Select only hospitals from selected state
hospitals <- outcome.care.measures[outcome.care.measures$State==state, ]
## Sort by lowest outcome and hospital name
hospitals <- hospitals[order(hospitals[, outcome.column.no], hospitals$Hospital.Name),]
## Return hospital name in that state with lowest 30-day death rate
if (num=="best") {
head(hospitals$Hospital.Name, 1)
} else if (num=="worst") {
tail(hospitals$Hospital.Name, 1)
} else {
hospitals[num, "Hospital.Name"]
}
} else {
stop("invalid state")
}
}
#rankhospital("TX", "heart failure", 4)
#rankhospital("MD", "heart attack", "worst")
#rankhospital("MN", "heart attack", 5000)