-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_04_rankall.R
199 lines (132 loc) · 6.04 KB
/
Problem_04_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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Problem 4, last problem
# Tidy up from rankhospital...
detach("package:data.table", unload = TRUE)
rm(PneumoniaRanks, FailureRanks, AttackRanks, outcomes, outcomes.split, getRank)
library(tidyverse)
# Read in our data
outcomes <- data.table::fread("C:/Users/corma/OneDrive/Documents/R/Assignment3/hospitaldata/outcomes.csv",
header = TRUE, sep = ",", stringsAsFactors = FALSE,
na.strings = "Not Available", select = c(2,7,11,17,23)) %>%
naniar::replace_with_na_all(condition = ~.x %in% "Not Available") %>% # Doing it with :: removes the need to load another package we're only going to use for one function call one time.
as_tibble %>%
mutate(across(3:5, as.numeric)) %>%
mutate(across(2, as.factor)) %>%
data.table::setnames(c("Hospital","State","Heart Attack","Heart Failure","Pneumonia"))
# Create our list of data frames split by state
outcomes.split <- split(outcomes, outcomes$State)
# Create our outcome ranks vectors...
getRank <- function(df, column.name) {
ColumnRank <- rank(df[[column.name]], ties.method = "first", na.last = "keep")
ColumnRank
}
PneumoniaRanks <- sapply(X = outcomes.split, FUN = getRank, column.name = "Pneumonia")
AttackRanks <- sapply(X = outcomes.split, FUN = getRank, column.name = "Heart Attack")
FailureRanks <- sapply(X = outcomes.split, FUN = getRank, column.name = "Heart Failure")
# And bind these vectors back to their respective data frames...
for (i in 1:length(outcomes.split)) {outcomes.split[[i]] <- cbind(outcomes.split[[i]], PneumoniaRanks[[i]])
colnames(outcomes.split[[i]])[ncol(outcomes.split[[i]])] <- "PneumoniaRank"
}
for (i in 1:length(outcomes.split)) {outcomes.split[[i]] <- cbind(outcomes.split[[i]], AttackRanks[[i]])
colnames(outcomes.split[[i]])[ncol(outcomes.split[[i]])] <- "AttackRank"
}
for (i in 1:length(outcomes.split)) {outcomes.split[[i]] <- cbind(outcomes.split[[i]], FailureRanks[[i]])
colnames(outcomes.split[[i]])[ncol(outcomes.split[[i]])] <- "FailureRank"
}
# Now to coalesce it all back into one large data frame:
outcomes2 <- unsplit(outcomes.split, outcomes$State)
data(mtcars)
temp <- split(mtcars, mtcars$cyl)
unsplittemp <- unsplit(temp, mtcars$cyl)
# That was easy thanks to data.table.
rankedoutcomes <- data.table::rbindlist(outcomes.split)
# But I don't want a data.table....
rankedoutcomes <- as_tibble(rankedoutcomes)
str(rankedoutcomes)
# New function...
library(dplyr)
testoutput1 <- rankedoutcomes %>%
filter(.[[6]] == 1) %>%
.[c(1,2,6)]
testoutput1
testoutput20 <- rankedoutcomes %>%
filter(.[[6]] == 20) %>%
.[c(1,2,6)]
testoutput20
# The trick is getting it to return NAs for states where there is no 20... I'll think
# about it tomorrow, or later tonight. # Later: I think to make this easier will mean we should use outcomes.split
# not rankedoutcomes
#I might not actually want to coalesce the data honestly, might be better to keep it as 54 separate
# data frames
# Probably want to write a for loop for this...
rankall <- function(outcome, num) {
# Let's first get our invalid outcome stop message out of the way...
if(!(outcome %in% c("Heart Failure","Heart Attack","Pneumonia")))
stop("invalid outcome")
# Create an empty data frame to store the results
results <- data.frame(hospital = character(),
state = factor(),
stringsAsFactors = FALSE)
# Create that index number...
if(outcome == "Heart Attack"){index = 7}
else if (outcome == "Heart Failure"){index = 8}
else if (outcome == "Pneumonia"){index = 5}
# Wow that was so much cleaner than how I did it in the other function
# Now our for loop
for (i in 1:length(outcomes.split)) {
worstdeterminant <- outcomes.split[[i]][index]
if (num == "best") {
z = 1
} else if (num == "worst") {
z = max(na.omit(worstdeterminant[,1]))
} else {
z = num
}
statesubset <- subset(outcomes.split[[i]], outcomes.split[[i]][index] == z)[1:2]
if (nrow(statesubset) == 0) {
results <- rbind(results, data.frame(Hospital = NA, State = names(outcomes.split[i])))
}
# Not sure why I needed to capitalize Hospital and State here, but hey, it worked.
else {results <- rbind(results, statesubset)}
}
rownames(results) <- NULL
data.table::setnames(results, c("hospital","state"))
results
}
rankall("Heart Failure",1)
rankall("Heart Failure","best")
rankall("Heart Failure","worst")
rankall("Heart Failure",150)
7 %in% c(1,2,7,4)
7 %in% c(1,2,4)
"Dog" %in% c(1, "cat",1:50, "Dog")
"Heart Attack" %in% c("Heart Failure","Heart Attack","Pneumonia")
paste0(disease, "Rank")
?subset
names(outcomes.split[7])
# Just CT
names(outcomes.split[[7]])
# Gives you the entire 7th state data frame - Connecticut
# Names = the names of the columns
outcomes.split[[7]][7]
# Gives you just the 7th column of the Connecticut data frame - attackRank
subset(outcomes.split[[7]], outcomes.split[[7]][7] == 5)
# Gives you only the entire row in the Connecticut subset where the 7th column's value for that row is 5.
# I.E. where heart attack rank is 5.
subset(outcomes.split[[7]], outcomes.split[[7]][7] == 5)[1:2]
#Gives you just the first two columns of that subset, i.e. just the hospital name and state abbreviation.
index <- 7
names(outcomes.split)
names(outcomes.split[7])
stateoutcomesubset <- outcomes.split[[7]][7]
max(na.omit(stateoutcomesubset[,1]))
stateoutcomesubset[, 1]
stateoutcomesubset
testframe1 <- data.table::setnames(data.frame(hospital = NA, state = "CT"), c("Applesauce","Aardvark"))
testframe1
testframe2 <- data.table::setnames(data.frame(hospital = NA, state = names(outcomes.split[7])), c("Applesauce","Aardvark"))
testframe2
<<<<<<< HEAD
CTSubset <- outcomes.split[[7]]
colnames(CTSubset)
=======
>>>>>>> 4e07ea465e30a9f291f72584592c0eafa4851a26