-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbest.R
More file actions
43 lines (27 loc) · 1.04 KB
/
Copy pathbest.R
File metadata and controls
43 lines (27 loc) · 1.04 KB
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
best <- function(state, outcome) {
directory <- "data"
outcomes_data <- c("heart attack", "heart failure", "pneumonia")
for (letters in list.files()) {
if(identical(letters, directory)){
setwd(directory)
}
}
outcome_data <- read.csv("outcome-of-care-measures.csv", colClasses = "character", na.strings = "Not Available")
if(!(state %in% outcome_data$State)) {
stop("invalid state")
}
if(!any(sapply(outcomes_data, function(x) identical(x, outcome)))) {
stop("invalid outcome")
}
outcome_column <- NULL
if(outcome == outcomes_data[1]) {
outcome_column <- 11
} else if(outcome == outcomes_data[2]) {
outcome_column <- 17
} else {
outcome_column <- 23
}
outcome_data_state <- outcome_data[outcome_data[,7] == state,]
min_indexes <- which.min(as.double(outcome_data_state[,outcome_column]))
sort(as.character(outcome_data_state[min_indexes, 2]))[1]
}