-
Notifications
You must be signed in to change notification settings - Fork 0
/
Medication tables.R
executable file
·172 lines (146 loc) · 6.78 KB
/
Medication tables.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
# Table 2a,2b and supplementary 3a,3b
# medicine wise table - gest wise and med wise
library(tibble)
library(readxl)
########## Dose #########
rm(list=ls())
dose1 = read_excel('Site1_Neofax_Training.xlsx',sheet = 1)
dose = rbind(dose1, read_excel('Site2_Neofax_Training.xlsx',sheet = 1))
dose = dose[-1,] #row not needed
dose = dose[-(nrow(dose1)),] #removed first blank column which was generated while appending second cehtre's baseline_data
write.csv(dose, 'freq.csv')
dose = read.csv('freq.csv')
dose = add_column(dose, GESTATION = (dose$Gestation.Weeks + dose$Gestation.Days/7), .after = 4)
dose$GESTATION = as.character(dose$GESTATION)
#removing outliers
outliers = which(dose$GESTATION<26)
dose = dose[-(outliers),]
gestation_cat1 = dose[which(dose$GESTATION < 32),]
gestation_cat2 = dose[which(dose$GESTATION >= 32 & dose$GESTATION <= 34),]
gestation_cat3 = dose[which(dose$GESTATION > 34 & dose$GESTATION <= 37),]
gestation_cat4 = dose[which(dose$GESTATION > 37),]
data_name = c('gestation_cat1', 'gestation_cat2', 'gestation_cat3', 'gestation_cat4')
column_index = grep('Medicine', colnames(dose))
antibiotics = c()
for (i in 1:250) {
antibiotics = c(antibiotics, unique(as.character(dose[,column_index[i]])))
}
antibiotics = unique(antibiotics)
antibiotics = antibiotics[!is.na(antibiotics)]
temp_table = c()
allcolsofgest = c()
for (gest_category in 1:length(data_name)){
temp_table =c()
temp_data = get(data_name[gest_category])
dose_error = 0
no_dose_error = 0
total_doses = 0
patients_with_error = 0
patients_without_error = 0
dose_error_percentage = 0
for (i in 1:length(antibiotics)) {
dose_error[i] = 0
no_dose_error[i] = 0
total_doses[i] = 0
patients_with_error[i] = 0
patients_without_error[i] = 0
for (j in 1:250) {
error = 0
no_error = 0
patients = 0
patients_no_error = 0
filter_data = temp_data[which(temp_data[,column_index[j]] == antibiotics[i]),]
error = sum(filter_data[which(filter_data[,column_index[j]+5] == 'Deviation'),column_index[j]+7])
no_error = sum(filter_data[which(filter_data[,column_index[j]+5] == 'No Error'),column_index[j]+7])
patients = nrow(filter_data[which(filter_data[,column_index[j]+5] == 'Deviation'),])
patients_no_error = nrow(filter_data[which(filter_data[,column_index[j]+5] == 'No Error'),])
dose_error[i] = dose_error[i] + error
no_dose_error[i] = no_dose_error[i] + no_error
total_doses[i] = dose_error[i] + no_dose_error[i]
patients_with_error[i] = patients_with_error[i] + patients
patients_without_error[i] = patients_without_error[i] + patients_no_error
dose_error_percentage[i] = round(((dose_error[i] / (dose_error[i] + no_dose_error[i]))*100),digits=2)
}
something = cbind(dose_error[i], total_doses[i])
temp_table = rbind(temp_table, something)
}
allcolsofgest = cbind(allcolsofgest, temp_table)
}
# temp_result = cbind(antibiotics, dose_error, no_dose_error, dose_error_percentage, patients_with_error, patients_without_error)
# dose_result = cbind(antibiotics, dose_error, total_doses)
tabletemp1 = cbind(antibiotics, allcolsofgest)
tabletemp2 = c("", "Dose deviations", "Total doses given", "Dose deviations", "Total doses given", "Dose deviations", "Total doses given", "Dose deviations", "Total doses given")
tabletemp3 = c("Gestations", "<32", "<32", "32-34","32-34", "34-37", "34-37", ">37", ">37")
final_table_med = rbind(tabletemp3, tabletemp2, tabletemp1)
view(final_table_med)
#### Frequency ####
rm(list=ls())
freq1 = read_excel('Site1_Neofax_Training.xlsx',sheet = 2)
freq = rbind(freq1, read_excel('Site2_Neofax_Training.xlsx',sheet = 2))
freq = freq[-1,] #row not needed
freq = freq[-(nrow(freq1)),] #removed first blank column which was generated while appending second cehtre's baseline_data
write.csv(freq, 'freq.csv')
freq = read.csv('freq.csv')
freq = add_column(freq, GESTATION = (freq$Gestation.Weeks + freq$Gestation.Days/7), .after = 4)
freq$GESTATION = as.character(freq$GESTATION)
#removing outliers
outliers = which(freq$GESTATION<26)
freq = freq[-(outliers),]
gestation_cat1 = freq[which(freq$GESTATION < 32),]
gestation_cat2 = freq[which(freq$GESTATION >= 32 & freq$GESTATION <= 34),]
gestation_cat3 = freq[which(freq$GESTATION > 34 & freq$GESTATION <= 37),]
gestation_cat4 = freq[which(freq$GESTATION > 37),]
data_name = c('gestation_cat1', 'gestation_cat2', 'gestation_cat3', 'gestation_cat4')
column_index = grep('Medicine', colnames(freq))
antibiotics = c()
for (i in 1:250) {
antibiotics = c(antibiotics, unique(as.character(freq[,column_index[i]])))
}
antibiotics = unique(antibiotics)
antibiotics = antibiotics[!is.na(antibiotics)]
temp_table = c()
allcolsofgest = c()
for (gest_category in 1:length(data_name)){
temp_table =c()
temp_data = get(data_name[gest_category])
freq_error = 0
no_freq_error = 0
total_freqs = 0
patients_with_error = 0
patients_without_error = 0
freq_error_percentage = 0
for (i in 1:length(antibiotics)) {
freq_error[i] = 0
no_freq_error[i] = 0
total_freqs[i] = 0
patients_with_error[i] = 0
patients_without_error[i] = 0
for (j in 1:250) {
error = 0
no_error = 0
patients = 0
patients_no_error = 0
filter_data = temp_data[which(temp_data[,column_index[j]] == antibiotics[i]),]
error = sum(filter_data[which(filter_data[,column_index[j]+5] == 'Deviation'),column_index[j]+7])
no_error = sum(filter_data[which(filter_data[,column_index[j]+5] == 'No Error'),column_index[j]+7])
patients = nrow(filter_data[which(filter_data[,column_index[j]+5] == 'Deviation'),])
patients_no_error = nrow(filter_data[which(filter_data[,column_index[j]+5] == 'No Error'),])
freq_error[i] = freq_error[i] + error
no_freq_error[i] = no_freq_error[i] + no_error
total_freqs[i] = freq_error[i] + no_freq_error[i]
patients_with_error[i] = patients_with_error[i] + patients
patients_without_error[i] = patients_without_error[i] + patients_no_error
freq_error_percentage[i] = round(((freq_error[i] / (freq_error[i] + no_freq_error[i]))*100),digits=2)
}
something = cbind(freq_error[i], total_freqs[i])
temp_table = rbind(temp_table, something)
}
allcolsofgest = cbind(allcolsofgest, temp_table)
}
# temp_result = cbind(antibiotics, freq_error, no_freq_error, freq_error_percentage, patients_with_error, patients_without_error)
# freq_result = cbind(antibiotics, freq_error, total_freqs)
tabletemp1 = cbind(antibiotics, allcolsofgest)
tabletemp2 = c("", "freq deviations", "Total freqs given", "freq deviations", "Total freqs given", "freq deviations", "Total freqs given", "freq deviations", "Total freqs given")
tabletemp3 = c("Gestations", "<32", "<32", "32-34","32-34", "34-37", "34-37", ">37", ">37")
final_table_med = rbind(tabletemp3, tabletemp2, tabletemp1)
view(final_table_med)