-
Notifications
You must be signed in to change notification settings - Fork 0
/
rankhospital.r
45 lines (37 loc) · 1.81 KB
/
rankhospital.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
## Find the hospital in a state with the given rank for an outcome
rankhospital <- function(state, outcome, num = "best", directory = "./hospital_compare_data") {
# Create list of outcome-column number pairs in
# 'outcome-of-care_measures.csv'
outcomeList <- list(c("heart attack", "heart failure", "pneumonia"),
c(11, 17, 23))
names(outcomeList) <- c("measure", "column")
# Match 'outcome' with column number; throw error if length == 0
column <- outcomeList$column[outcomeList$measure == tolower(outcome)]
if (length(column) == 0) stop("invalid outcome")
# Read 'outcome-of-care-measures.csv' from 'directory'
# Coerce all data into character class
outcomeData <- read.csv(paste0(directory, "/outcome-of-care-measures.csv"),
colClass = "character")
# Subset 'outcomeData' to rows with 'state'; throw error if nrows == 0
outcomeData <- subset(outcomeData, toupper(state) == State)
if(nrow(outcomeData) == 0) stop("invalid state")
# Limit and re-order 'outcomeData' to 'outcome', 'name'
outcomeData <- outcomeData[, c(column, 2)]
names(outcomeData) <- c("outcome", "name")
# Remove NA 'outcome' rows
outcomeData <- subset(outcomeData, !is.na(as.numeric(outcome)))
# Determine number of measurements and save to 'numMax'
numMax <- nrow(outcomeData)
# Test 'num' for non-numeric and out-of-range values
num <- if(is.numeric(num)) {
if(num < 1 | num > numMax) {
return(NA)
} else num
} else if(tolower(num) == "best") {
1
} else if(tolower(num) == "worst") {
numMax
} else return(NA)
# Order by 'outcome' then 'name' and return row 'num'
outcomeData[order(as.numeric(outcomeData[[1]]), outcomeData[[2]]), 2][num]
}