-
Notifications
You must be signed in to change notification settings - Fork 1
/
rankhospital.R
35 lines (29 loc) · 842 Bytes
/
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
rankhospital <- function(state, outcome, num){
data <- .GlobalEnv$outcome
if (!(state %in% data$State)) {
stop("invalid state")
}
colNum <- if (outcome == "heart attack") {
11
} else if (outcome == "heart failure") {
17
} else if (outcome == "pneumonia") {
23
} else {
stop("invalid outcome")
}
data <- data[order(data$Hospital.Name),]
data <- data[which((data[,colNum])!="Not Available"),]
data[,colNum] <- as.numeric(data[,colNum])
hospitals <- data[which (data$State == state),]
hospitals <- hospitals[order(hospitals[,colNum]),]
rank <- if (num == "best"){
1
} else if (num == "worst"){
nrow(hospitals)
} else {
num
}
winner <- hospitals[rank,"Hospital.Name"]
winner
}