-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_clean.qmd
306 lines (261 loc) · 8.42 KB
/
02_clean.qmd
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
---
title: "02_clean"
format: html
editor: visual
---
```{r}
library(tidyverse)
```
```{r}
dat_load_01 <- read_tsv(
file = '~/projects/Group_19_project/data/01_dat_load.tsv'
)
```
# Joining demonstration
Joining since we did not need to group together two data set. We won't use the joined data set for the rest of the project,
it is just a demonstration of our joining skills
## Addition of an observation ID
```{r}
dat_load_01 <- dat_load_01 |>
mutate(ID = row_number()) |>
relocate(ID)
```
## Separation of the dataset in two distinct datasets
```{r}
df_Etiology <- dat_load_01 |> # First dataset containing etiologies
select(ID,
cps,
Alcohol,
hcv,
hbv,
nafld,
Cryptogenic,
PrimarySclerosingCholangitis,
Autoimmunehepatitis,
pbc,
Otherliverdiseaseetiology
)
df_Comorbidities <- dat_load_01 |> # Second dataset containing comorbidities
select(ID,
mi,
chf,
pvd,
Cerebrovasculardisease,
Dementia,
Chronicpulmonorydisease,
Rheumatologicaldisease,
Pepticulcerdisease,
Anymalignancyincludinglymphomaor,
Moderateorsevereliverdisease,
aids
)
```
## Joining the two datasets
```{r}
df_Etiology_and_Comorbidities <- left_join(df_Etiology, df_Comorbidities)
df_Etiology_and_Comorbidities
```
# Cleaning of the dataset
```{r}
dat_clean_02 <- dat_load_01 |>
# Selection of correct variable
select(Sex,
`Age(Decade)atadmission`,
Liverrelateddiagnosis1,
Alcohol,
hcv,
hbv,
nafld,
Cryptogenic,
PrimarySclerosingCholangitis,
Autoimmunehepatitis,
pbc,
mi,
chf,
pvd,
Cerebrovasculardisease,
Dementia,
Chronicpulmonorydisease,
Rheumatologicaldisease,
Pepticulcerdisease,
Mildliverdisease,
Diabetes,
Diabeteswithchroniccomplications,
Heiplegiaorparaplegia,
Renaldisease,
Anymalignancyincludinglymphomaor,
Moderateorsevereliverdisease,
Metastaticsolidtumor,
aids,
cps,
Mortality_InHospital,
CHARLSON_CI,
MORTALITY_30_DAYS_postDC,
MORTALITY_90_DAYS_postDC ) |>
# Create a general diabetes variable with and without chronic complications
mutate(C_Diabetes_with_without_complications = Diabetes +
Diabeteswithchroniccomplications) |>
# Discard the previous diabetes variables
select(-c(Diabetes, Diabeteswithchroniccomplications)) |>
# Change the name of variables
rename(Age = `Age(Decade)atadmission`,
Liver_disease = Liverrelateddiagnosis1,
E_Alcohol_consumption = Alcohol,
E_Hepatitis_c = hcv,
E_Hepatitis_b = hbv,
E_Non_alcoolic_fatty_LD = nafld,
E_Cryptogenic = Cryptogenic,
E_Primary_sclerosing_cholangitis = PrimarySclerosingCholangitis,
E_Autoimmune_hepatitis = Autoimmunehepatitis,
E_Primary_biliary_cholangitis = pbc,
C_Myocardial_infarction = mi,
C_Congestive_Heart_Failure = chf,
C_Peripheral_vascular_disease = pvd,
C_Cerebro_Vascular_disease = Cerebrovasculardisease,
C_Dementia = Dementia,
C_Chronic_pulmonary_disease = Chronicpulmonorydisease,
C_Rheumatological_disease = Rheumatologicaldisease,
C_Pepticulcer_disease = Pepticulcerdisease,
C_Mild_liver_disease = Mildliverdisease,
C_Heiplegia_paraplegia = Heiplegiaorparaplegia,
C_Renal_disease = Renaldisease,
C_Malignancy = Anymalignancyincludinglymphomaor,
C_Moderate_severe_liver_disease = Moderateorsevereliverdisease,
C_Metastatic_solid_tumor = Metastaticsolidtumor,
C_AIDS = aids,
CPS = cps,
Mortality_hospital = Mortality_InHospital,
Charlson_CI = CHARLSON_CI,
Mortality_30_days = MORTALITY_30_DAYS_postDC,
Mortality_90_days = MORTALITY_90_DAYS_postDC,
) |>
# Reordering columns
relocate(C_Diabetes_with_without_complications, .after = C_AIDS) |>
relocate(Charlson_CI, .after = CPS) |>
# Add a column "C_none"
mutate(C_None = (C_AIDS == 0) &
(C_Cerebro_Vascular_disease ==0)&
(C_Congestive_Heart_Failure == 0) &
(C_Chronic_pulmonary_disease == 0) &
(C_Dementia == 0) &
(C_Diabetes_with_without_complications == 0) &
(C_Heiplegia_paraplegia == 0) &
(C_Malignancy == 0) &
(C_Myocardial_infarction == 0) &
(C_Mild_liver_disease == 0) &
(C_Moderate_severe_liver_disease == 0) &
(C_Metastatic_solid_tumor == 0) &
(C_Pepticulcer_disease == 0) &
(C_Peripheral_vascular_disease == 0) &
(C_Rheumatological_disease == 0) &
(C_Renal_disease == 0)
) |>
# Change numerical (1/0) into categorical (Yes/No)
mutate_at(vars(starts_with('C_')), ~ifelse(. == 1, 'Yes', 'No')) |>
mutate_at(vars(starts_with('E_')), ~ifelse(. == 1, 'Yes', 'No')) |>
mutate_at(vars(starts_with('Mortality')), ~ifelse(. == 1, 'Yes', 'No')) |>
# If mortalityH = yes -> Mortality 30&90 = no
mutate_at(vars(c(Mortality_30_days, Mortality_90_days)),
~ifelse(Mortality_hospital == 'Yes', 'No', .)) |>
# If mortality30 = yes -> mortality90 = no
mutate_at(vars(Mortality_90_days),
~ifelse(Mortality_30_days == 'Yes', 'No', .)) |>
# Create a id for each ind
mutate(Id = row_number()) |>
relocate(Id, .before = Sex) |>
relocate(C_None, .before = CPS) |>
# Getting rid of observations with NA values: data set goes from 733 patients
# to 468 patients which is still acceptable
drop_na(CPS) |>
drop_na(Sex) |>
drop_na(Mortality_hospital) |>
drop_na(Mortality_30_days) |>
drop_na(Mortality_90_days)
dat_clean_02
```
## Tidying of the data using pivot_longer - Only tidying the comorbidities
Tidying only comorbidities to be able to count them properly in 04_describe
```{r}
dat_tidy_C_02 <- dat_clean_02 |>
# Create a column Comorbidities using the pivot_longer (one column /
# one line / one observation)
pivot_longer(
cols = starts_with("C_"),
names_to = "Comorbidities",
values_to = "Presence_of_comorbidities",
values_drop_na = TRUE) |>
filter(Presence_of_comorbidities == 'Yes') |>
select(-Presence_of_comorbidities) |>
mutate(Comorbidities = str_remove(Comorbidities, "C_"))
dat_tidy_C_02
```
## Tidying of the data using pivot_longer - Only tidying the etiologies
```{r}
dat_tidy_E_02 <- dat_clean_02 |>
pivot_longer(
cols = starts_with("E_"),
names_to = "Etiologies",
values_to = "Presence_of_etiologies",
values_drop_na = TRUE) |>
filter(Presence_of_etiologies == 'Yes') |>
select(-Presence_of_etiologies) |>
mutate(Etiologies = str_remove(Etiologies, "E_"))
```
## Tidying of the data using pivot_longer
```{r}
dat_tidy_02 <- dat_clean_02 |>
# Create a column Comorbidities using the pivot_longer (one column /
# one line / one observation)
pivot_longer(
cols = starts_with("C_"),
names_to = "Comorbidities",
values_to = "Presence_of_comorbidities",
values_drop_na = TRUE) |>
# Create a column Etiology
pivot_longer(
cols = starts_with("E_"),
names_to = "Etiologies",
values_to = "Presence_of_etiologies",
values_drop_na = TRUE) |>
# Only select the patients that have comorbidity + the one that have
#C_none = Yes
filter(Presence_of_comorbidities == 'Yes') |>
select(-Presence_of_comorbidities) |>
filter(Presence_of_etiologies == 'Yes') |>
select(-Presence_of_etiologies) |>
# Change the name of the observations to make it more digest
mutate(Comorbidities = str_remove(Comorbidities, "C_")) |>
mutate(Etiologies = str_remove(Etiologies, "E_"))
dat_tidy_02
```
## Save data in the folder "data"
```{r}
write_tsv(
x= dat_clean_02,
file = '~/projects/Group_19_project/data/02_dat_clean.tsv'
)
```
```{r}
write_tsv(
x= dat_tidy_02,
file = '~/projects/Group_19_project/data/02_dat_tidy.tsv'
)
```
```{r}
write_tsv(
x= dat_tidy_C_02,
file = '~/projects/Group_19_project/data/02_dat_tidy_C.tsv'
)
```
```{r}
write_tsv(
x= dat_tidy_E_02,
file = '~/projects/Group_19_project/data/02_dat_tidy_E.tsv'
)
```
```{r}
write_tsv(
x= df_Etiology,
file = '~/projects/Group_19_project/data/02_df_E.tsv'
)
```