-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrankall.R
More file actions
45 lines (34 loc) · 1.28 KB
/
Copy pathrankall.R
File metadata and controls
45 lines (34 loc) · 1.28 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
44
45
rankall <- function(outcome, num = "best") {
Outcome.name <- c("heart attack", "heart failure", "pneumonia")
Outcome.col <- c(11, 17, 23)
foo <- data.frame(Outcome.name, Outcome.col)
## Check if outcome is valid
if(!(outcome %in% foo[,1])) stop("invalid outcome")
foo <- subset(foo, Outcome.name == outcome)
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
data[, foo[,2]] <- as.numeric(data[, foo[,2]])
data <- data[complete.cases(data[, foo[,2]]),]
## List all states
states <- unique(data[,7])
## Create the data frame to save the results
result <- data.frame()
for(state in states) {
# Filter data by state
state.data <- subset(data, data[, 7] == state)
# Order results by outcome and hospital name ASC
state.data <- state.data[ order(state.data[,foo[,2]], state.data[,2]), ]
if(num == "best") {
rank <- 1
} else if(num == "worst") {
rank <- nrow(state.data)
} else {
rank <- num
}
new.result <- data.frame(hospital = state.data[rank,2], state = state)
result <- rbind(result, new.result)
}
result$hospital <- as.character(result$hospital)
result$state <- as.character(result$state)
result <- result[order(result$state),]
}