-
Notifications
You must be signed in to change notification settings - Fork 0
/
best.R
49 lines (40 loc) · 1.68 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
best <- function(state,outcome){
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv",colClasses = "character")
## purge data of all columns not needed and rename column to match values
## this will make it easier to sort by column later
data <- data[,c(2,7,11,17,23)]
names(data)[3] <- "heart attack"
names(data)[4] <- "heart failure"
names(data)[5] <- "pneumonia"
## Check that state and outcome are valid
## Get all unique state abbreviations from dataset
allStates <- unique(data[,2])
## Tests if value provided to function is in the list of states
## If not return error message
if(is.element(state,allStates)){
validOutcomes <- c("heart attack","heart failure","pneumonia")
## Now we test for valid outcome. If valid proceed to look up data
## If not return error message
if(is.element(outcome,validOutcomes)){
##Trim down the data set to only what we need
switch(outcome,
"heart attack" = data <- data[data$State == state,1:3],
"heart failure" = data <- data[data$State == state,c(1:2,4)],
"pneumonia" = data <- data[data$State == state,c(1:2,5)])
## Make column 3 numeric
data[,3] <- suppressWarnings(as.numeric(data[,3]))
data <- data[order(data[,3],data[,1]),]
Hospital.Name <- data[1,1]
}
else{
stop("invalid outcome")
}
}
else{
stop("invalid state")
}
## Return hospital name in that state with lowest 30-day death
return(Hospital.Name)
## rate
}