forked from mbhushan/RProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrankall.R
More file actions
93 lines (86 loc) · 2.4 KB
/
rankall.R
File metadata and controls
93 lines (86 loc) · 2.4 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
rankall <- function(outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
outcome <- tolower(outcome)
## Check that state and outcome are valid
s <- data[,7]
s <- s[!is.na(s)]
s <- unique(s)
s <- s[order(s)]
v <- seq(11, 24, 6)
flag <- 0
coln <- 11
for (i in v) {
c <- colnames(data[i])
c <- gsub("\\."," ",c)
c <- tolower(c)
g <- grepl(outcome,c)
if (g) {
flag <- 1
coln <- i
break;
}
}
if (flag == 0) {
stop('invalid outcome')
}
df <- data.frame(hospital=character(0), state=character(0))
rc <- 1
for ( st in s) {
hosp = rankhospital(st, outcome, num)
df <- rbind(df, data.frame(hospital=hosp, state=st))
row.names(df)[rc] <- st
rc <- rc+1
#print(st)
}
## For each state, find the hospital of the given rank
## Return a data frame with the hospital names and the (abbreviated) state name
return (df)
}
rankhospital <- function(state, outcome, num = "best") {
## Read outcome data
## Check that state and outcome are valid
data <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
outcome <- tolower(outcome)
## Check that state and outcome are valid
x <- data[,7]
y <- x[!is.na(x)]
y <- as.vector(y)
if (!(state %in% y)) {
stop('invalid state')
}
v <- seq(11, 24, 6)
flag <- 0
coln <- 11
for (i in v) {
c <- colnames(data[i])
c <- gsub("\\."," ",c)
c <- tolower(c)
g <- grepl(outcome,c)
if (g) {
flag <- 1
coln <- i
break;
}
}
if (flag == 0) {
stop('invalid outcome')
}
## Return hospital name in that state with the given rank 30-day death rate
result <- c(NA)
df <- data.frame(hname=data[,2], state=data[,7], oc=data[,coln])
df <- df[df$state == state,]
df <- df[df$oc != "Not Available",]
score <- df[order(as.numeric(as.vector(df$oc)), df$hname), ]
nr <- nrow(score)
if (num == "best") {
num <- 1
} else if (num == "worst") {
num <- nr
}
if (num <= nr ) {
result <- as.vector(score[num,][1]$hname)
}
return (result)
}