-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrankhospital.R
56 lines (44 loc) · 1.5 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
52
53
54
55
56
library(data.table)
rankHospital <- function(state, outcome, num="best") {
# Read outcome data
dt <- data.table::fread("data/outcome-of-care-measures.csv")
# change outcome to lowercase
outcome <- tolower(outcome)
# change variable name to prevent confusion
chosen_state <- state
# Check state and outcome are valid, if not return warning message
if (!chosen_state %in% unique(dt[["State"]])) {
stop("Invalid state")
}
if (!outcome %in% c("heart attack", "heart failure", "pneumonia")) {
stop("Invalid outcome")
}
dt <- dt %>%
rename_with(~ tolower(gsub("^Hospital 30-Day Death \\(Mortality\\) Rates from ", "", .x))) %>%
filter(state == chosen_state) %>%
mutate(rate = suppressWarnings(as.numeric(get(outcome)))) %>%
clean_names() %>%
select(hospital_name, state, rate) %>%
filter(complete.cases(.)) %>%
arrange(rate, hospital_name) %>%
mutate(rank = row_number())
if (num == "best") {
unlist(head(dt[[1]], 1))
}
else if (num == "worst") {
unlist(tail(dt[[1]], 1))
}
else {
dt %>%
slice(num) %>%
select(hospital_name) %>%
unlist()
}
}
# sample outputs
rankHospital("TX", "heart failure", "best")
# 'FORT DUNCAN MEDICAL CENTER'
rankHospital("MD", "heart attack", "worst")
# 'HARFORD MEMORIAL HOSPITAL'
rankHospital("MN", "heart attack", 5000)
# rank that breaks limit returns nothing