-
Notifications
You must be signed in to change notification settings - Fork 0
/
best.R
53 lines (35 loc) · 1.59 KB
/
best.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
best <- function(state, outcome) {
## Read outcome data
outcomes <- read.csv("outcome-of-care-measures.csv",
colClasses = "character",
header = TRUE)
## Get data we're interested in
rates <- as.data.frame(cbind(outcomes[, 2], # hospital
outcomes[, 7], # state
outcomes[, 11], # heart attack
outcomes[, 17], # heart failure
outcomes[, 23]), # pneumonia
stringsAsFactors = FALSE)
## Rename columns
colnames(rates) <- c("hospital", "state", "heart attack", "heart failure", "pneumonia")
## Check that state and outcome are valid
if(!state %in% rates[,"state"]){
stop('invalid state')
}
if(!outcome %in% c("heart attack", "heart failure", "pneumonia")){
stop('invalid outcome')
}
## Return hospital name in that state with lowest 30-day death
## rate
## Get only the hospitals in chosen state
hRates <- rates[(rates[, "state"] == state), ]
## Convert outcome rate to numberic, gets a warning
hRates[, outcome] <- as.numeric(hRates[, outcome])
hRates <- hRates[!is.na(hRates[, outcome]), ]
## Order by outcome rate
hRates <- hRates[order(hRates[, outcome]), ]
## Get names of hospitlas with the lowest rate
hNames <- hRates[hRates[, outcome] == min(hRates[,outcome]),1]
## Sort by hospital name if tie
sort(hNames)[1]
}