-
Notifications
You must be signed in to change notification settings - Fork 0
/
rankall.R
66 lines (64 loc) · 2.85 KB
/
rankall.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
56
57
58
59
60
61
62
63
64
65
66
rankall <- function(state, outcome, rank) {
## Read outcome data
outcome.csv <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
outcome.csv[outcome.csv$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack == "Not Available",]$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack <- NA
outcome.csv[outcome.csv$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia == "Not Available",]$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia <- NA
outcome.csv[outcome.csv$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure == "Not Available",]$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure <- NA
newoutcome <- outcome.csv[,c(2,7,11,17,23)]
## Check that state and outcome are valid
state = toupper(state)
state_list <- unique(outcome.csv[,7])
outcome = tolower(outcome)
outcome_list <- c("heart attack", "pneumonia", "heart failure")
if (state %in% state_list == FALSE){
stop
geterrmessage("invalid state")
}
else if (outcome %in% outcome_list == FALSE){
stop
geterrmessage("invalid outcome")
}
## Return hospital name in that state with lowest 30-day death
## rate
else {
if (outcome == "heart attack"){
ha.mort.vals <- subset(newoutcome, State==state, select = c(State, Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, Hospital.Name))
ha.mort.nums <- subset(ha.mort.vals, is.na(as.numeric(ha.mort.vals[,2])) == FALSE)
hordered <- ha.mort.nums[order(ha.mort.nums[,2], ha.mort.nums[,3]),]
hordered <- cbind(hordered, 1:length(hordered[,2]))
if (rank == "worst"){
rank <- length(ha.mort.nums[,2])
}
else if (rank == "best"){
rank <- 1
}
hordered[rank, 3]
}
else if (outcome == "pneumonia"){
pneu.mort.vals <- subset(newoutcome, State==state, select = c(State, Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, Hospital.Name))
pneu.mort.nums <- subset(pneu.mort.vals, is.na(as.numeric(pneu.mort.vals[,2])) == FALSE)
pordered <- pneu.mort.nums[order(pneu.mort.nums[,2], pneu.mort.nums[,3]),]
pordered <- cbind(pordered, 1:length(pordered[,2]))
if (rank == "worst"){
rank <- length(pneu.mort.nums[,2])
}
else if (rank == "best"){
rank <- 1
}
pordered[rank, 3]
}
else if (outcome == "heart failure"){
hf.mort.vals <- subset(newoutcome, State==state, select = c(State, Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, Hospital.Name))
hf.mort.nums <- subset(hf.mort.vals, is.na(as.numeric(hf.mort.vals[,2])) == FALSE)
fordered <- hf.mort.nums[order(hf.mort.nums[,2], hf.mort.nums[,3]),]
fordered <- cbind(fordered, 1:length(fordered[,2]))
if (rank == "worst"){
rank <- length(hf.mort.nums[,2])
}
else if (rank == "best"){
rank <- 1
}
fordered[rank, 3]
}
}
}