forked from hqphat/coursera-r-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rankall.R
52 lines (45 loc) · 1.78 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
rankall <- function(outcome, num = "best") {
## Read outcome data
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
data <- data[c(2, 7, 11, 17, 23)]
names(data)[1] <- "name"
names(data)[2] <- "state"
names(data)[3] <- "heart attack"
names(data)[4] <- "heart failure"
names(data)[5] <- "pneumonia"
## Validate the outcome string
outcomes = c("heart attack", "heart failure", "pneumonia")
if( outcome %in% outcomes == FALSE ) stop("invalid outcome")
## Validate the num value
if( num != "best" && num != "worst" && num%%1 != 0 ) stop("invalid num")
## Grab only rows with data in our outcome
data <- data[data[outcome] != 'Not Available', ]
## Order the data
data[outcome] <- as.data.frame(sapply(data[outcome], as.numeric))
data <- data[order(data$name, decreasing = FALSE), ]
data <- data[order(data[outcome], decreasing = FALSE), ]
## Helper functiont to process the num argument
getHospByRank <- function(df, s, n) {
df <- df[df$state==s, ]
vals <- df[, outcome]
if( n == "best" ) {
rowNum <- which.min(vals)
} else if( n == "worst" ) {
rowNum <- which.max(vals)
} else {
rowNum <- n
}
df[rowNum, ]$name
}
## For each state, find the hospital of the given rank
states <- data[, 2]
states <- unique(states)
newdata <- data.frame("hospital"=character(), "state"=character())
for(st in states) {
hosp <- getHospByRank(data, st, num)
newdata <- rbind(newdata, data.frame(hospital=hosp, state=st))
}
## Return a data frame with the hospital names and the (abbreviated) state name
newdata <- newdata[order(newdata['state'], decreasing = FALSE), ]
newdata
}