-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_preprocessing_deaths.qmd
169 lines (145 loc) · 3.18 KB
/
04_preprocessing_deaths.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
---
title: "Pre-procesamiento de los datos sobre muertes"
author: "Brian Norman Peña-Calero"
date: "`r Sys.Date()`"
format:
html:
toc: true
number-sections: true
anchor-sections: true
code-line-numbers: true
code-overflow: scroll
code-copy: hover
code-link: true
code-fold: show
lang: es
embed-resources: true
editor_options:
chunk_output_type: console
---
# Importar paquetes
```{r}
library(tidyverse)
```
# Importar datos
```{r}
# Ruta al directorio donde se encuentran los archivos
path_dir1 <- "01_data/raw/Requested data for MS242/"
death_t1 <- purrr::map_df(
.x = c(
list.files(
path = path_dir1,
pattern = "DTHRED1.*\\.csv$",
full.names = TRUE
),
"01_data/raw/Data Request MS242_06072024/DTHRED1PE_L1AD_20240521.csv"
),
.f = readr::read_csv
)
death_t2 <- purrr::map_df(
.x = c(
list.files(
path = path_dir1,
pattern = "DTHRED2.*\\.csv$",
full.names = TRUE
),
"01_data/raw/Data Request MS242_06072024/DTHRED2PE_L1AD_20240521.csv"
),
.f = readr::read_csv
)
death_t3 <- purrr::map_df(
.x = c(
list.files(
path = path_dir1,
pattern = "DTHRED3.*\\.csv$",
full.names = TRUE
),
"01_data/raw/Data Request MS242_06072024/DTHRED3PE_L1AD_20240521.csv"
),
.f = readr::read_csv
)
```
```{r}
ghe_t1 <- read_csv("01_data/raw/ghe_codes_tier1.csv")
ghe_t2 <- read_csv("01_data/raw/ghe_codes_tier2.csv")
ghe_t3 <- read_csv("01_data/raw/ghe_codes_tier3.csv")
```
```{r}
ghe <- bind_rows(
ghe_t1,
ghe_t2,
ghe_t3
) %>%
select(DTHCOD_FINAL = group,
GTHGROUP = name) %>%
distinct() %>%
add_case(
DTHCOD_FINAL = "0000",
GTHGROUP = "Overall"
) %>%
drop_na()
```
# Resumen de datos por años
```{r}
death_t1 %>%
arrow::arrow_table()
```
```{r}
deaths <- death_t1 %>%
select(ISO2, SALID1, YEAR,
DTHCOD_FINAL = DTHCOD_FINAL1,
deaths = DTHRED1DEATHS) %>%
bind_rows(
death_t2 %>%
select(ISO2, SALID1, YEAR,
DTHCOD_FINAL = DTHCOD_FINAL2,
deaths = DTHRED2DEATHS),
death_t3 %>%
select(ISO2, SALID1, YEAR,
DTHCOD_FINAL = DTHCOD_FINAL3,
deaths = DTHRED3DEATHS),
death_t1 %>%
summarise(
deaths = sum(DTHRED1DEATHS, na.rm = TRUE),
.by = c(ISO2, SALID1, YEAR)
) %>%
mutate(DTHCOD_FINAL = 0),
.id = "Tier"
)
deaths <- deaths %>%
mutate(
Tier = factor(Tier,
labels = c("T1", "T2",
"T3", "Overall")),
DTHCOD_FINAL = str_pad(DTHCOD_FINAL,
pad = "0",
width = 4)
)
deaths_ghe <- deaths %>%
left_join(ghe,
by = join_by(DTHCOD_FINAL))
```
```{r}
#| eval: false
arrow::write_parquet(
deaths_ghe,
"01_data/processed/deaths_ghe.parquet"
)
arrow::write_ipc_stream(
deaths_ghe,
"01_data/processed/deaths_ghe.arrow"
)
```
```{r}
deaths_by_year <- deaths_ghe %>%
summarise(
deaths = sum(deaths),
.by = c(Tier, ISO2, SALID1,
YEAR, DTHCOD_FINAL,
GTHGROUP)
)
```
# Exportar datos
```{r}
write_csv(deaths_by_year, file = "01_data/processed/deaths_by_year.csv")
```