-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrankall.R
77 lines (63 loc) · 2.66 KB
/
rankall.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
66
67
68
69
70
71
72
73
74
75
76
77
library(data.table)
rankAll <- function(outcome, num = "best") {
# Read outcome data
dt <- data.table::fread("data/outcome-of-care-measures.csv")
# change outcome to lowercase
outcome <- tolower(outcome)
# check if outcome is valid
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))) %>%
mutate(rate = suppressWarnings(as.numeric(get(outcome)))) %>%
clean_names() %>%
select(hospital_name, state, rate) %>%
filter(complete.cases(.)) %>%
group_by(state) %>%
arrange(rate, hospital_name, .by_groups=TRUE) %>%
arrange(state) %>%
mutate(rank = row_number())
if (num == "best") {
dt %>%
filter(rank == 1) %>%
select(hospital_name, state)
}
else if (num == "worst") {
dt %>%
group_by(state) %>%
filter(rank == max(rank)) %>%
select(hospital_name, state)
}
else {
dt %>%
group_by(state) %>%
filter(rank == num) %>%
select(hospital_name, state)
}
}
## sample outputs
head(rankAll("heart attack", 20), 5)
## hospital state
## D W MCMILLAN MEMORIAL HOSPITAL AL
## ARKANSAS METHODIST MEDICAL CENTER AR
## JOHN C LINCOLN DEER VALLEY HOSPITAL AZ
## SHERMAN OAKS HOSPITAL CA
## SKY RIDGE MEDICAL CENTER CO
tail(rankAll("pneumonia", "worst"), 3)
## hospital state
## MAYO CLINIC HEALTH SYSTEM - NORTHLAND, INC WI
## PLATEAU MEDICAL CENTER WV
## NORTH BIG HORN HOSPITAL DISTRICT WY
tail(rankAll("heart failure"), 10)
## hospital_name state
## WELLMONT HAWKINS COUNTY MEMORIAL HOSPITAL TN
## FORT DUNCAN MEDICAL CENTER TX
## VA SALT LAKE CITY HEALTHCARE - GEORGE E. WAHLEN VA MEDICAL CENTER UT
## SENTARA POTOMAC HOSPITAL VA
## GOV JUAN F LUIS HOSPITAL & MEDICAL CTR VI
## SPRINGFIELD HOSPITAL VT
## HARBORVIEW MEDICAL CENTER WA
## AURORA ST LUKES MEDICAL CENTER WI
## FAIRMONT GENERAL HOSPITAL WV
## CHEYENNE VA MEDICAL CENTER WY