-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankall.R
45 lines (37 loc) · 1.13 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
rankall <- function(outcome, num = "best") {
data <- read.csv("data/outcome-of-care-measures.csv", colClasses = "character")
data$State <- as.factor(data$State)
possible_outcomes = c(11, 17, 23)
names(possible_outcomes) <- c("heart attack", "heart failure", "pneumonia")
if (!(outcome %in% names(possible_outcomes))) {
stop("invalid outcome")
}
outcome <- possible_outcomes[outcome]
data <- data[, c(2, 7, outcome)]
names(data) <- c("hospital", "state", "outcome")
data$outcome <- as.numeric(data$outcome)
data <- data[order(data$state, data$outcome, data$hospital), ]
s <- split(data, data$state)
l <- lapply(s, get_hospital_by_rank, num)
d <- data.frame(hospital=unlist(l), state=names(l))
d
}
get_hospital_by_rank <- function(data, num) {
rank <- NA
if (is.character(num)) {
if (num == "best") {
rank <- 1
} else if (num == "worst") {
data <- data[!is.na(data$outcome), ]
rank <- dim(data)[1]
}
} else if (is.numeric(num)) {
if (num >= 1 & num <= dim(data)[1]) {
rank <- num
}
}
if (is.na(rank)) {
return(rank)
}
data$hospital[rank]
}