-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest.R
55 lines (54 loc) · 2.71 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
54
55
best <- function(state, outcome) {
data <- read.csv("rprog3/outcome-of-care-measures.csv")[, c(7, 2,11, 17, 23)]
posout <- c("heart attack", "heart failure", "pneumonia")
posstates <- as.character(levels(as.factor(data$State)))
besthospital <- character()
if (!match(state, posstates, nomatch = FALSE)) {
return(warning("Invalid State"))
}
else if (!match(outcome, posout, nomatch = FALSE)) {
return(warning("Invalid Outcome"))
}
else {
if (outcome == "heart attack") {
data1 <- data[, 1:3]
data1[[3]] <- as.numeric(data1[[3]])
good <- complete.cases(data1)
good.data <- data1[good,]
statesplit <- split(good.data, good.data$State)
alphasplit <- as.data.frame(statesplit[state])
betasplit <- alphasplit[order(alphasplit[[2]]),]
charliesplit <- betasplit[[3]]
minrate <- min(charliesplit)
ind <- match(minrate, charliesplit)
besthospital <- betasplit[[2]][ind]
}
else if (outcome == "heart failure") {
data1 <- data[, c(1,2,4)]
data1[[3]] <- as.numeric(data1[[3]])
good <- complete.cases(data1)
good.data <- data1[good,]
statesplit <- split(good.data, good.data$State)
alphasplit <- as.data.frame(statesplit[state])
betasplit <- alphasplit[order(alphasplit[[2]]),]
charliesplit <- betasplit[[3]]
minrate <- min(charliesplit)
ind <- match(minrate, charliesplit)
besthospital <- betasplit[[2]][ind]
}
else if (outcome == "pneumonia") {
data1 <- data[, c(1,2,5)]
data1[[3]] <- as.numeric(data1[[3]])
good <- complete.cases(data1)
good.data <- data1[good,]
statesplit <- split(good.data, good.data$State)
alphasplit <- as.data.frame(statesplit[state])
betasplit <- alphasplit[order(alphasplit[[2]]),]
charliesplit <- betasplit[[3]]
minrate <- min(charliesplit)
ind <- match(minrate, charliesplit)
besthospital <- betasplit[[2]][ind]
}
}
return(besthospital)
}