-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrankall.R
executable file
·44 lines (37 loc) · 1.08 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
rankall <- function(outcome,num='best') {
data<-read.csv('outcome-of-care-measures.csv')
outcomes<-c('heart attack', 'heart failure', 'pneumonia')
indices<-c(11,17,23)
if (!outcome %in% outcomes) stop('invalid outcome')
i<-indices[match(outcome,outcomes)]
hospitals<-data[, c(2,7,i)]
hospitals[,3]<-as.numeric(as.character(hospitals[,3]))
hospitals<-na.omit(hospitals)
names(hospitals) <- c('hospital','state','rate')
if (num == "best") {
num <- 1
} else if (num == "worst") {
num <- nrow(hospitals)
} else {
num <- as.numeric(num)
if (is.na(num)) {
stop('invalid num')
} else if (num > nrow(hospitals)) {
return(NA)
}
}
results <- NULL
for(state in levels(hospitals$state)) {
hospitals_for_state <- hospitals[hospitals$state == state,]
if (num == "worst") {
n <- nrow(hospitals_for_state)
} else {
n <- num
}
result <- hospitals_for_state[order(hospitals_for_state$rate, hospitals_for_state$hospital), c(1,2)][n,]
result$state <- rep(state,nrow(result))
results <- rbind(results, result)
}
rownames(results) <- NULL
return(results)
}