-
Notifications
You must be signed in to change notification settings - Fork 0
/
rankhospital.R
39 lines (30 loc) · 1.15 KB
/
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
36
37
38
rankhospital <- function(state, outcome, num)
{
data <- read.csv("outcome-of-care-measures.csv", na.strings="Not Available", stringsAsFactors=FALSE )
validOutcomes <- c("heart attack", "heart failure", "pneumonia")
if (!(state %in% (data[,7])))
{
stop("invalid state")
}
if ((outcome %in% validOutcomes) == FALSE)
{
stop("invalid outcome")
}
colIndex <- (if (outcome == "heart attack") {11}
else if (outcome == "heart failure") {17}
else {23})
filteredData <- data[,c(2,7,colIndex)]
filteredData <- na.omit(object = filteredData, columns = 3)
filteredStateData <- subset(filteredData, State == state)
orderedData <- filteredStateData[order( filteredStateData[,3], filteredStateData[,1]),]
if (num == "best")
{
num = 1
}
if (num == "worst")
{
num = nrow(orderedData)
}
selectedHospital <- orderedData[num,1]
selectedHospital
}